這裏是我的請求的身份驗證令牌代碼...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和消息被訪問不想。