谷歌硬盘将错误400或403返回到我的Android应用程序?

时间:2012-09-06 16:36:06

标签: android google-drive-api

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

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:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

问题似乎是在API控制台中我没有在服务下启用 DRIVE SDK DRIVE API 。它们是分开的,一个不会自动转向另一个。