收到来自 authTOken 的来自 Google Drive sdk 的刷新令牌
RefreshToken from GoogleDrive sdk from authTOken received
我正在尝试集成 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
驱动器?
不确定这有多大帮助。但是你可以看看这段代码,看看它是否有帮助。
https://github.com/sDurgam/koszt/blob/master/sTestJSONTables/src/com/example/s_expensemanager/MainActivity.java
Google 仅驱动 returns 在初始登录期间交换 OAuth 令牌代码的刷新令牌。
refresh_token (optional) This field is only present if access_type=offline is included in the authentication request. For details, see Refresh tokens.
见Google API documentation。这讨论了如何向我们发送 http 请求而不是 java 对象,但我认为这些对象可以在同一个庄园中使用。
我正在尝试集成 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
驱动器?
不确定这有多大帮助。但是你可以看看这段代码,看看它是否有帮助。 https://github.com/sDurgam/koszt/blob/master/sTestJSONTables/src/com/example/s_expensemanager/MainActivity.java
Google 仅驱动 returns 在初始登录期间交换 OAuth 令牌代码的刷新令牌。
refresh_token (optional) This field is only present if access_type=offline is included in the authentication request. For details, see Refresh tokens.
见Google API documentation。这讨论了如何向我们发送 http 请求而不是 java 对象,但我认为这些对象可以在同一个庄园中使用。