【发布时间】:2015-05-19 02:02:53
【问题描述】:
我正在尝试通过 google 进行身份验证,我目前正在使用他的文档中推荐的方式,但是.. 有没有简单的方法来获取刷新令牌?,我进行身份验证并获取令牌,但我无法获取刷新令牌,我需要 id。
试了很多方法,这个问题我已经花了一个多星期了,有没有可能拿到那个token?我试过很多手册、教程……但我做不到。
任何人都知道我可以在任何地方知道如何获取 resfresh_token 并且很好解释并且目前正在工作?
非常感谢!!
Pd:是一个原生的安卓应用。
编辑: 好的,有关更多信息,我正在按照谷歌文档中的方式进行身份验证,以使用 GoogleApiClient 进行身份验证,但变化不大(因为我将其用作经理)。这部分运行正常:
我调用创建而不是调用:
public void logginGooglePlus(GooglePlusAuthCallback googlePlusAuthCallback) {
gPAuthCallback = googlePlusAuthCallback;
// Initializing google plus api client
String scope = "audience:server:client_id:" + SERVER_CLIENT_ID;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN).build();
mGoogleApiClient.connect();
mSignInClicked = true;
signInWithGplus(gPAuthCallback);
}
我继续使用谷歌的复制和粘贴:
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
// GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(),
// this,
// 0).show();
if (gPAuthCallback != null) {
gPAuthCallback.onLoginError(result.toString());
}
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
@Override
public void onActivityResult(int requestCode, int responseCode,
Intent intent) {
super.onActivityResult(requestCode, responseCode, intent);
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
// Get user's information
if (gPAuthCallback != null) {
gPAuthCallback.onLoginSuccesful();
}
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
}
/**
* Sign-in into google
* */
public void signInWithGplus(GooglePlusAuthCallback googlePlusAuthCallback) {
gPAuthCallback = googlePlusAuthCallback;
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
/**
* Method to resolve any signin errors
* */
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
最后我打电话来获取人员数据:
public void getProfileInformation(
GooglePlusGetPersonCallback getPersonCallback) {
this.googlePlusGetPersonCallback = getPersonCallback;
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personPhotoUrl = currentPerson.getImage().getUrl();
String personGooglePlusProfile = currentPerson.getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Log.e("GPlus", "Name: " + personName + ", plusProfile: "
+ personGooglePlusProfile + ", email: " + email
+ ", Image: " + personPhotoUrl);
new getTokenAsyncTask().execute();
}
} catch (Exception e) {
if (googlePlusGetPersonCallback != null) {
// googlePlusGetPersonCallback.ongeGooglePersonError(e.getCause()
// .toString());
}
e.printStackTrace();
}
}
好的,离开这里很容易,现在开始有趣的部分:我需要刷新令牌,因为我必须使用服务器登录,并且我必须传递 access_token、refresh_token 和 user_id。
阅读:https://developers.google.com/accounts/docs/CrossClientAuth
我知道我必须使用不同的 Scope 进行 getToken 调用,所以我更改了它:get token 的方法是:
// GET TOKEN 2o plano
public class getTokenAsyncTask extends AsyncTask<Void, Boolean, String> {
@Override
protected String doInBackground(Void... params) {
try {
String acountname = Plus.AccountApi
.getAccountName(mGoogleApiClient);
// agregamos el scope del server para que me loguee para la app
// "crossclient"
String serverScope = "audience:server:client_id:"
+ SERVER_CLIENT_ID;
String token = GoogleAuthUtil.getToken(GooglePlusManager.this,
acountname, serverScope);
return token;
} catch (UserRecoverableAuthException e) {
// startActivityForResult(e.getIntent(), "NECESITA AUT");
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace(); // TODO: handle the exception
}
return null;
}
@Override
protected void onPostExecute(String code) {
String token = code;
if (googlePlusGetPersonCallback != null) {
googlePlusGetPersonCallback.ongeGooglePersonSuccesful(
currentPerson, token);
}
}
}
根据文档,我会得到一个令牌:“ID 令牌将包含多个数据字段”,我只是检索一个字符串令牌(但它不会导致任何崩溃或问题,所以我想没关系)。我无法访问服务器,但我认为没关系,因为 ios 应用程序已经运行正常(另一家公司在 Ios 中完成了它),我是否必须要求他们在服务器中进行任何操作这样我就可以向服务器验证我的 android 应用程序了吗?
ios 应用程序正在向服务器传递我已经说过的参数(访问权限、引用、id)所以我想象我必须在 android 中传递相同的参数,我可以访问控制台并且我已经声明了 android应用在同一个项目中。
好吧,从我的角度来看,我有一个假定的有效令牌。我怎样才能获得刷新令牌?我完全迷路了......
如果有人知道如何获得它。我会尽可能多地邀请啤酒(我为此浪费了很多时间:S)。
为这个非常大的帖子道歉 :((这是我的第一篇!)。
【问题讨论】:
-
显示不起作用的代码可能会对您有所帮助
标签: android authentication google-plus google-oauth