谷歌驱动器返回错误400或403到我的Andr​​oid应用程序? [英] Google Drive Returning Error 400 or 403 to my Android App?

查看:222
本文介绍了谷歌驱动器返回错误400或403到我的Andr​​oid应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code请求的身份验证令牌...

Here's my code for requesting the auth token...

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.

这里的code处理该令牌:

Here's the code that handles the token:

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并将它设置为登录请求code和结果code,如果它曾经被调用,但它永远不会是。被它永远被调用,它只是触发了另一个令牌请求如果请求code是3025,结果code是RESULT_OK。

I have implemented onActivityResult and have it set up to log the requestCode and resultCode if it's ever invoked, but it never is. Were it to ever be invoked, it just fires off another token request if the requestCode is 3025 and the resultCode is RESULT_OK.

下面就是我得到了我的clientID的:

Here's how I got my clientID:


  • 我去了谷歌的API控制台或任何它被称为。

  • 我做了一个新的产品。

  • 我启用并设置驱动器的SDK(有点......我不是做一个web应用程序,所以我只是指着一个网站,我自己的网址。)

  • 在API访问我点击创建另一个客户端ID ...,并将其设置为已安装的Andr​​oid应用程序。 (指纹可能不是很正确?那会导致错误?)

  • 我复制下客户端ID列出安装的应用程序的客户端ID(而不是一个来自客户端ID为驱动SDK)。

正因为如此,这里就是我从日志中获得:

As it is, here's what I get from the log:

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"
 }
}

随之而来的是更多的普通堆栈跟踪...

Followed by a more ordinary stack trace...

修改
最近,它停止了给我400,而是开始给我403紫禁城,仍与usageLimits的领域,原因正在accessNotConfigured和消息被访问没有想通。

EDIT Recently it stopped giving me 400 and instead started giving me 403 Forbidden, still with the domain of usageLimits, reason now being accessNotConfigured and message being Access Not Figured.

推荐答案

这个问题似乎是,在API控制台中我没有打开双方的 DRIVE SDK DRIVE API 下的服务。他们是独立的,一个不会自动开启其他的。

The issue appears to have been that in APIs Console I hadn't turned on both DRIVE SDK and DRIVE API under services. They're separate, and one doesn't automatically turn the other on.

这篇关于谷歌驱动器返回错误400或403到我的Andr​​oid应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆