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の場合、別のトークン要求が発生します。

clientIDを取得する方法は次のとおりです。

  • 私はGoogleAPIコンソールまたはそれが呼ばれるものに行きました。
  • 新製品を作りました。
  • Drive SDKを有効にしてセットアップしました(Webアプリを作成していないので、自分が所有するWebサイトにURLを指定しただけです)。
  • [APIアクセス]で[別のクライアントIDを作成...]をクリックし、インストールされているAndroidアプリ用に設定しました。(指紋が正しくない可能性がありますか?それがエラーの原因になりますか?)
  • インストールされているアプリケーションのクライアントIDの下にリストされているクライアントIDをコピーしました(ドライブ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であり、メッセージはAccessNotFiguredです。

4

2 に答える 2

2

APIコンソールから取得したクライアントIDをに渡しますが、driveRequest.setKeyそのメソッドは、クライアントIDではなくAPIキーの設定に使用されます。そのため、アプリケーションは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コンソールでサービスの下でDRIVESDKDRIVEAPIの両方をオンにしていないことであるようです。それらは別々であり、一方が他方を自動的にオンにすることはありません。

于 2012-09-07T15:45:20.583 に答える