收到来自auth TOken的Google Drive sdk刷新令牌

时间:2015-02-28 06:39:16

标签: java android eclipse google-drive-api

我正在尝试整合GoogleDriveSDK

这是我的代码

mGoogleClient = new GoogleApiClient.Builder(this)
        .addApi(Drive.API)
        .addScope(Drive.SCOPE_FILE)
        .addConnectionCallbacks(new ConnectionCallbacks() 
        {
            @Override
            public void onConnectionSuspended(int arg0) {
            }

            @Override
            public void onConnected(Bundle bundle) 
            {               
                AccountManager am = AccountManager.get(AddAccountActivity.this);
                am.getAuthToken(am.getAccounts()[0], "oauth2:" + DriveScopes.DRIVE, new Bundle(), AddAccountActivity.this, 
                        new OnTokenAcquired(), null);
            }
        })
        .addOnConnectionFailedListener(new OnConnectionFailedListener() 
        {
            @Override
            public void onConnectionFailed(ConnectionResult connectionResult) 
            {
                if (connectionResult.hasResolution()) 
                {
                    try {
                            connectionResult.startResolutionForResult(AddAccountActivity.this, ACTIVITY_RESULT_GOOGLE);
                        } catch (IntentSender.SendIntentException e) 
                        {}
                } 
                else 
                    GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), AddAccountActivity.this, 0).show();
            }
        })
        .build();
        mGoogleClient.connect();

private class OnTokenAcquired implements AccountManagerCallback<Bundle> 
    {
        @Override
        public void run(AccountManagerFuture<Bundle> result) 
        {
            try {
                final String token = result.getResult().getString(AccountManager.KEY_AUTHTOKEN);


                Intent launch = (Intent)result.getResult().get(AccountManager.KEY_INTENT);
                if (launch != null) {
                    startActivityForResult(launch, 3025);
                    return; // Not sure why... I wrote it here for some reason. Might not actually be necessary.
                }
            } catch (OperationCanceledException e) {
                // Handle it...
            } catch (AuthenticatorException e) {
                // Handle it...
            } catch (IOException e) {
                // Handle it...
            }
        }
    }   

现在的问题是,我不知道如何从authToken获取RefreshToken并保存以供以后使用。

其次,如何将此refreshtoken保存到initialise云端硬盘?

2 个答案:

答案 0 :(得分:0)

不确定这有多大帮助。但是你可以查看这段代码,看看它是否有帮助。 https://github.com/sDurgam/koszt/blob/master/sTestJSONTables/src/com/example/s_expensemanager/MainActivity.java

答案 1 :(得分:0)

Google云端硬盘仅在初次登录时返回刷新令牌,以交换OAuth令牌的代码。

  

refresh_token(可选)仅当身份验证请求中包含access_type = offline时,才会显示此字段。有关详细信息,请参阅刷新令牌。

请参阅Google API documentation。这里讨论了如何使用http请求而不是java对象,但我认为这些对象可以在同一个庄园中使用。