Android应用程序:获取Google Drive API的访问令牌 [英] Android App: Acquire Access Token for Google Drive API

查看:425
本文介绍了Android应用程序:获取Google Drive API的访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Android(版本ICS)应用程序。将数据上传到Google云端硬盘。应用程序
使用oauth2来获取访问令牌。

字符串AUTH_TOKEN_TYPE =

字符串AUTH_TOKEN_TYPE =的oauth2:HTTPS://www.googleapis.com/auth/drive;
//步骤1
accountManager.getAuthToken(
账户,//使用getAccountsByType(com.google)获取的账户
AUTH_TOKEN_TYPE,//认证令牌类型
options,// Authenticator特定的选项
this,//你的活动
new OnTokenAcquired(),//成功获取令牌时调用的回调函数
new Handler(new OnAuthTokenError())) ; //发生错误时调用回调
}
private class OnTokenAcquired implements AccountManagerCallback< Bundle> {
@Override
public void run(AccountManagerFuture< Bundle> result){
//从AccountManagerFuture获取操作的结果。
捆绑包;
尝试{
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:访问令牌的交换授权令牌。

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

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

try {//步骤2:交换一个访问刷新令牌
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();
}
返回null;
}
}

失败。 LogCat显示如下:
com.google.api.client.auth.oauth2.TokenResponseException:400错误请求


{

 error:unauthorized_client

}



我可以使用云端硬盘应用在我的Android平板电脑上访问Google云端硬盘。所以
我的电子邮件帐户是有效的。可能是AUTH_TOKEN_TYPE不正确,但Google Drive
SDK不清楚它必须是什么。我缺少什么?

解决方案

您无需执行交换令牌的第二步。 Android直接授予您访问令牌,它不会向您授予您必须换取令牌的授权码。

.android.com / training / id-auth / authenticate.htmlrel =nofollow>关于Android文档的这个页面解释了一切。


I am writing an Android (version ICS) app. which uploads data to the Google Drive. The app uses oauth2 to acquire the access token.

First step: acquire authorization token.

    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();
        }
    }
} 

Success. An authorization token is acquired.

Step 2: Exchange authorization token for 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;
    }
}  

Fail. The LogCat shows the following: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request

{

 "error":"unauthorized_client"

}

I have been able to access "Google Drive" on my Android tablet using the "Drive" app. so my email account is valid. May be the AUTH_TOKEN_TYPE is incorrect, but the Google Drive SDK is not clear what it must be. What am I missing?

解决方案

You do not need to do the second step of exchanging the token. Android grants you an access token directly, it does not grant you an auth code which you would have to exchange for tokens.

This page on the Android documentation explains everything really well.

这篇关于Android应用程序:获取Google Drive API的访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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