3

私はAndroid(バージョンICS)アプリを書いています。Google ドライブにデータをアップロードします。アプリは oauth2 を使用してアクセス トークンを取得します。

最初のステップ: 認証トークンを取得します。

    String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive";
    // Step 1
    accountManager.getAuthToken(
        account,                                // Account retrieved using getAccountsByType("com.google")
        AUTH_TOKEN_TYPE,                        // Auth Token Type
        options,                                // Authenticator-specific options
        this,                                   // Your activity
        new OnTokenAcquired(),                  // Callback called when a token is successfully acquired
        new Handler(new OnAuthTokenError()));   // Callback called if an error occurs
}
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
    @Override
    public void run(AccountManagerFuture<Bundle> result) {
        // Get the result of the operation from the AccountManagerFuture.
        Bundle bundle;
        try {
            bundle = result.getResult();

            authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);

            Log.d(TAG,"authToken:" + authToken);

            exchangeToken access = (exchangeToken) new exchangeToken().execute();

        } catch (OperationCanceledException e) {
            e.printStackTrace();
        } catch (AuthenticatorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 

成功。認可トークンを取得します。

ステップ 2: Access Token の認証トークンを交換します。

    private class exchangeToken extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {
        HttpTransport transport = new NetHttpTransport();
        JsonFactory jsonFactory = new GsonFactory();
        String CLIENT_ID = "999999999999.apps.googleusercontent.com";
        String CLIENT_SECRET = "axXXXXXXXXXXXXXXX7";

        try { // Step 2: Exchange for an access and refresh token
            GoogleTokenResponse authResponse = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, authToken, CALLBACK_URL).execute();
            accessToken = authResponse.getAccessToken();
            Log.d("Get Access","Token:" + accessToken);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}  

失敗。LogCat には次のように表示されます: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request

{

 "error":"unauthorized_client"

}

「ドライブ」アプリを使用して、Android タブレットで「Google ドライブ」にアクセスできました。私のメールアカウントは有効です。AUTH_TOKEN_TYPE が正しくない可能性がありますが、Google Drive SDK はそれが何である必要があるのか​​明確ではありません。私は何が欠けていますか?

4

2 に答える 2

1

トークンを交換する 2 番目のステップを実行する必要はありません。Android はアクセス トークンを直接付与します。トークンと交換する必要がある認証コードは付与しません。

Android ドキュメントのこのページでは、すべてが非常によく説明されています。

于 2012-05-25T20:49:11.507 に答える
0

Drive API を使用するには、ユーザーがアプリを Chrome(!) ウェブストアにインストールする必要があることをご存知ですか? 通常、Documents List API は Android からのより適切な選択です。

于 2012-05-26T08:14:12.813 に答える