2012-09-06 36 views
1

這裏是我的請求的身份驗證令牌代碼...Google Drive將錯誤400或403返回到我的Android應用程序?

AccountManager am = AccountManager.get(this); 
Bundle options = new Bundle(); 
am.getAuthToken(
    (am.getAccounts())[0], // My test device only has one account. I'll add a picker before releasing this app. 
    "oauth2:" + DriveScopes.DRIVE, 
    options, 
    true, // I've tried both true and false... doesn't seem to change anything? 
    new OnTokenAcquired(), 
    null); // I used to have a handler, but it absolutely never got called. 

下面是處理令牌代碼:

private class OnTokenAcquired implements AccountManagerCallback<Bundle> { 
    @Override 
    public void run(AccountManagerFuture<Bundle> result) { 
     String token = result.getResult().getString(AccountManager.KEY_AUTHTOKEN); 

     HttpTransport httpTransport = new NetHttpTransport(); 
     JacksonFactory jsonFactory = new JacksonFactory(); 

     Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null); 
     b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() { 
      @Override 
      public void initialize(JsonHttpRequest request) throws IOException { 
       DriveRequest driveRequest = (DriveRequest) request; 
       driveRequest.setPrettyPrint(true); 
       driveRequest.setKey("xxxxxxxxxxxx.apps.googleusercontent.com"); // I replaced the number with x's. I'll explain where I got the number from below. 
       driveRequest.setOauthToken(token); 
      } 
     }); 

     final Drive drive = b.build(); 

     final com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File(); 
     body.setTitle("My document"); 
     body.setDescription("A test document"); 
     body.setMimeType("text/plain"); 

     java.io.File fileContent = new java.io.File("document.txt"); 
     final FileContent mediaContent = new FileContent("text/plain", fileContent); 

     new Thread(new Runnable() { 
      public void run() { 
       com.google.api.services.drive.model.File file; 
       try { 
        file = drive.files().insert(body, mediaContent).execute(); 
        Log.i("Hi", "File ID: " + file.getId()); 
       } catch (IOException e) { 
        Log.i("Hi", "The upload/insert was caught, which suggests it wasn't successful..."); 
        e.printStackTrace(); 
        AccountManager am = AccountManager.get(activity); 
        am.invalidateAuthToken(am.getAccounts()[0].type, null); 
        Bundle options = new Bundle(); 
        am.getAuthToken(
         (am.getAccounts())[0], 
         "oauth2:" + DriveScopes.DRIVE, 
         options, 
         true, 
         new OnTokenAcquired(), 
         null); 
        } 
       } 
      }).start(); 
     Intent launch = (Intent)result.getResult().get(AccountManager.KEY_INTENT); 
     if (launch != null) { 
      Log.i("Hi", "Something came back as a KEY_INTENT"); 
      startActivityForResult(launch, 3025); 
      return; 
     } else { 
      Log.i("Hi", "I checked, but there was nothing for KEY_INTENT."); 
     } 
    } 
} 

我已經實現onActivityResult,並將它設置爲登錄的requestCode和發送resultCode如果它曾經被調用過,但它永遠不會。如果它被調用,它只是在requestCode爲3025且resultCode爲RESULT_OK的情況下觸發另一個令牌請求。

這裏就是我得到了我的clientID的:

  • 我去谷歌API控制檯或任何它被稱爲。
  • 我做了一個新產品。
  • 我啓用並設置了Drive SDK(有點......我沒有製作一個網絡應用程序,所以我只是把它指向了我自己的網站。)
  • 在「API訪問」下,我點擊「創建另一個客戶端ID ...「併爲已安裝的Android應用程序進行設置。 (指紋可能不太正確?是否會導致錯誤?)
  • 我複製了客戶端ID(安裝應用程序的客戶端ID)下列出的客戶端ID(不是來自Drive SDK客戶端ID的客戶端ID)。

正因爲如此,這裏就是我從日誌中獲得:

I checked, but there was nothing for KEY_INTENT // My log message. 
The upload/insert was caught, which suggests it didn't work... // Another one of my log messages. 
com.google.api.client.http.HttpResponseException: 400 Bad Request 
{ 
"error": { 
    "errors": [ 
    { 
    "domain": "usageLimits", 
    "reason": "keyInvalid", 
    "message": "Bad Request" 
    } 
    ], 
    "code": 400, 
    "message": "Bad Request" 
} 
} 

隨之而來的是更多的普通堆棧跟蹤...

編輯 最近就停止給我400而是開始給我403 Forbidden,仍然使用usageLimits的領域,現在的原因是accessNotConfigured和消息被訪問不想。

回答

0

這個問題似乎是,在API控制檯中我沒有打開這兩個DRIVE SDK下服務DRIVE API。他們是分開的,一個不會自動打開另一個。

2

您將從API控制檯獲得的客戶端ID傳遞到driveRequest.setKey,但是該方法用於設置API密鑰而不是客戶端ID。這就是爲什麼您的應用程序沒有正確綁定到API控制檯的項目,並且您的應用程序不允許使用該API。

一旦您獲得該客戶端ID標識的項目的有效OAuth令牌,即可通過調用driveRequest.setOauthToken(String)進行設置。

有關OAuth2用戶在Android上檢查的詳細信息http://developer.android.com/training/id-auth/authenticate.html

相關問題