我想为我的Android应用启用服务器端Calendar API访问。
我已按照here给出的步骤。
我获取授权码的空值。
我认为我为'范围'提供了错误的价值观。字段和' server_client_id'字段。
请举例说明'范围'的正确字段值。和' server_client_id'在getToken()方法中。
感谢您的帮助。
PS-我已经使用谷歌+登录安卓[{3}}用于连接到谷歌帐户。
编辑 - 这是我的代码。我已在范围字段中为Google Calendar API提供了OAuth 2.0范围。
我已经从Developers Console获取了Android应用程序的客户端ID,并放入了' serverClientID'领域。这可能是我错的地方。我不知道如何获取
所需的服务器客户端IDpublic class AsyncGetAuthToken extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
Bundle appActivities = new Bundle();
appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
"MainActivity");
String scopeString = "https://www.googleapis.com/auth/calendar.readonly";
String serverClientID = CLIENT_ID;
String scopes = "oauth2:server:client_id:" + serverClientID + ":api_scope:" + scopeString;
String code = null;
try {
code = GoogleAuthUtil.getToken(
MainActivity.this, // Context context
Plus.AccountApi.getAccountName(mGoogleApiClient), // String accountName
scopes, // String scope
appActivities // Bundle bundle
);
} catch (IOException transientEx) {
// network or server error, the call is expected to succeed if you try again later.
// Don't attempt to call again immediately - the request is likely to
// fail, you'll hit quotas or back-off.
return null;
} catch (UserRecoverableAuthException e) {
// Requesting an authorization code will always throw
// UserRecoverableAuthException on the first call to GoogleAuthUtil.getToken
// because the user must consent to offline access to their data. After
// consent is granted control is returned to your activity in onActivityResult
// and the second call to GoogleAuthUtil.getToken will succeed.
startActivityForResult(e.getIntent(), AUTH_CODE_REQUEST_CODE);
return null;
} catch (GoogleAuthException authEx) {
// Failure. The call is not expected to ever succeed so it should not be
// retried.
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
}
在我的onActivityResult中,我寻找Auth Code
if (requestCode == AUTH_CODE_REQUEST_CODE) {
if (responseCode == RESULT_OK){
Bundle extra = intent.getExtras();
String oneTimeToken = extra.getString("authtoken");
Log.d("LOG", "one time token" + oneTimeToken);
}
}