我使用应用程序,它必须连接到谷歌,获取accessToken并将其发送到服务器端。在服务器端,此令牌用于连接到gmail帐户并获取所有联系人。这是我的代码和平,我如何获得访问令牌:
private static final String SCOPE = "oauth2: https://www.googleapis.com/auth/userinfo.profile" +
" https://www.googleapis.com/auth/userinfo.email" +
" https://www.googleapis.com/auth/plus.login";
static final int REQUEST_CODE_PICK_ACCOUNT = 1000;
static final int REQUEST_CODE_RECOVER_FROM_AUTH_ERROR = 1001;
static final int REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR = 1002;
String mEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_social_auth);
pickUserAccount();
}
/** Starts an activity in Google Play Services so the user can pick an account */
private void pickUserAccount() {
String[] accountTypes = new String[]{"com.google"};
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
accountTypes, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
if (resultCode == RESULT_OK) {
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
new AccessToken(this, mEmail, SCOPE).execute();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "You must pick an account", Toast.LENGTH_SHORT).show();
}
} else if ((requestCode == REQUEST_CODE_RECOVER_FROM_AUTH_ERROR ||
requestCode == REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR)
&& resultCode == RESULT_OK) {
handleAuthorizeResult(resultCode, data);
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void handleAuthorizeResult(int resultCode, Intent data) {
if (data == null) {
show("Unknown error, click the button again");
return;
}
if (resultCode == RESULT_OK) {
Log.i(TAG, "Retrying");
mEmail = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
new AccessToken(this, mEmail, SCOPE).execute();
return;
}
if (resultCode == RESULT_CANCELED) {
show("User rejected authorization.");
return;
}
show("Unknown error, click the button again");
}
/**
* This method is a hook for background threads and async tasks that need to provide the
* user a response UI when an exception occurs.
*/
public void handleException(final Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (e instanceof GooglePlayServicesAvailabilityException) {
// The Google Play services APK is old, disabled, or not present.
// Show a dialog created by Google Play services that allows
// the user to update the APK
int statusCode = ((GooglePlayServicesAvailabilityException)e)
.getConnectionStatusCode();
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(statusCode,
GoogleAuthActivity.this,
REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
dialog.show();
} else if (e instanceof UserRecoverableAuthException) {
// Unable to authenticate, such as when the user has not yet granted
// the app access to the account, but the user can fix this.
// Forward the user to an activity in Google Play services.
Intent intent = ((UserRecoverableAuthException)e).getIntent();
startActivityForResult(intent,
REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
}
}
});
}
public void show(final String message) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
finish();
}
});
}
class AccessToken extends AsyncTask<Void, Void, String> {
GoogleAuthActivity context;
String email;
String scope;
AccessToken(GoogleAuthActivity context, String email, String scope) {
this.context = context;
this.email = email;
this.scope = scope;
}
@Override
protected String doInBackground(Void... params) {
String token = null;
try {
token = GoogleAuthUtil.getToken(context, email, scope);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present, which is
// recoverable, so we need to show the user some UI through the activity.
// короче жопа((
context.handleException(userRecoverableException);
} catch (IOException e) {
e.printStackTrace();
} catch (GoogleAuthException e) {
e.printStackTrace();
}
return token;
}
@Override
protected void onPostExecute(String s) {
sendToServer(s);
}
}
服务器开发人员使用此 url 获取访问权限:https://www.googleapis.com/plus/v1/people/[id]/people/visible?access_token=[myAccessToken]但是在服务器端,我收到 403 错误:未配置访问权限。未为您的项目启用 API,或者您的 API 密钥上配置了每 IP 或每引用限制,并且请求与这些限制不匹配。请使用 Google 开发者控制台更新您的配置。
如果我仅使用 https://www.googleapis.com/auth/userinfo.profile 作为 SCOPE,我们会收到下一个错误:"权限不足"
在开发者控制台中,我启用了联系人 API 和 Google+ API。
我想从 android 设计中获取有用的访问令牌,将其发送到服务器端,并从中获取用户联系人。我做错了什么?
您是否访问了 Google 开发人员控制台并为您的项目启用了Contacts
API?