1

这是我请求身份验证令牌的代码...

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。

以下是我获得客户 ID 的方式:

  • 我去了 Google APIs 控制台或其他任何名称。
  • 我做了一个新产品。
  • 我启用并设置了 Drive SDK(有点……我不是在制作网络应用程序,所以我只是将 URL 指向我拥有的网站。)
  • 在“API 访问”下,我单击“创建另一个客户端 ID...”并为已安装的 Android 应用程序进行设置。(指纹可能不太对?那会导致错误吗?)
  • 我复制了已安装应用程序的客户端 ID 下列出的客户端 ID(不是 Drive SDK 的客户端 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,消息是Access Not Figd。

4

2 回答 2

2

您将从 API 控制台获得的客户端 ID 传递给driveRequest.setKey,但是该方法用于设置 API 密钥而不是客户端 ID。这就是为什么您的应用程序没有从 API 控制台正确绑定到项目并且您的应用程序不允许使用 API 的原因。

一旦您获得了由该客户端 ID 标识的项目的有效 OAuth 令牌,您可以通过调用driveRequest.setOauthToken(String).

有关 Android 上 OAuth2 的更多详细信息,请查看http://developer.android.com/training/id-auth/authenticate.html

于 2012-09-07T15:16:15.717 回答
0

问题似乎是在 API 控制台中,我没有在服务下同时打开DRIVE SDKDRIVE API。它们是分开的,一个不会自动打开另一个。

于 2012-09-07T15:45:20.583 回答