使用Google Plus和android应用程序进行身份验证,如何获取刷新令牌
我想通过谷歌认证,我目前正在使用他文档中推荐的方式,但是。。有什么简单的方法可以获得刷新令牌吗?我进行了身份验证并获得了令牌,但是我无法获取刷新令牌,我需要id 我已经尝试了很多方法,我已经花了一个多星期的时间处理这个问题,有可能得到那个代币吗?我尝试了很多手册、教程。。。但我不能 任何人都知道我可以在哪里获得resfresh_代币,这是很好的解释,目前正在工作 非常感谢 Pd:是一款原生android应用程序 编辑: 好的,要了解更多信息,我正在使用谷歌文档中的auth来使用GoogleAppClient进行auth,几乎没有任何变化(因为我将它用作经理的助手)。这部分没问题: Firs,而不是调用create I调用:使用Google Plus和android应用程序进行身份验证,如何获取刷新令牌,android,authentication,google-plus,google-oauth,Android,Authentication,Google Plus,Google Oauth,我想通过谷歌认证,我目前正在使用他文档中推荐的方式,但是。。有什么简单的方法可以获得刷新令牌吗?我进行了身份验证并获得了令牌,但是我无法获取刷新令牌,我需要id 我已经尝试了很多方法,我已经花了一个多星期的时间处理这个问题,有可能得到那个代币吗?我尝试了很多手册、教程。。。但我不能 任何人都知道我可以在哪里获得resfresh_代币,这是很好的解释,目前正在工作 非常感谢 Pd:是一款原生android应用程序 编辑: 好的,要了解更多信息,我正在使用谷歌文档中的auth来使用GoogleApp
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();
}
}
好的,离开这里很容易,现在开始有趣的部分:我需要刷新令牌,因为我必须登录服务器,我必须传递访问令牌、刷新令牌和用户id
阅读此文:
我知道我必须使用不同的作用域进行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);
}
}
}
//获取令牌2o plano
公共类getTokenAsyncTask扩展异步任务{
@凌驾
受保护字符串doInBackground(无效…参数){
试一试{
字符串acountname=Plus.AccountApi
.getAccountName(mgoogleapClient);
//agregamos el scope del server para que me loguee para la app
//“跨客户端”
String serverScope=“访问群体:服务器:客户端\u id:”
+服务器\客户端\ ID;
String token=GoogleAuthUtil.getToken(GooglePlusManager.this,
acountname,serverScope);
返回令牌;
}捕获(UserRecoverableAuthe异常){
//startActivityForResult(如getIntent(),“NECESITA AUT”);
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();//TODO:处理异常
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串代码){
字符串标记=代码;
if(googlePlusGetPersonCallback!=null){
googlePlusGetPersonCallback.ongegooglepersonSuccessful(
当前人、令牌);
}
}
}
根据文档,我将得到一个标记:“ID标记将包含多个数据字段”,并且我只检索一个字符串标记(但它不会导致任何崩溃或问题,所以我认为它是可以的)。我还没有访问服务器,但我认为它是正常的,因为ios应用程序已经正常运行了(另一家公司已经在ios中运行了),我是否必须要求他们在服务器中进行任何操作,以便我可以通过服务器验证我的android应用程序
ios应用程序正在向服务器传递我已经说过的参数(acces,refres,id),所以我想我必须在android中传递相同的参数,我可以访问控制台,并且我已经在同一个项目中声明了android应用程序
嗯,从我的身份来看,我有一个假定的有效代币。。如何获取刷新令牌?我完全迷路了
如果有人知道如何得到它。。我会请你尽可能多的啤酒(我已经浪费了这么多时间和这个:S)
xcuses for the really really big post:((这是我的第一篇!)。根据,你可以交换代币
所以,若您将所需参数发布到下面的链接,那个么您将获得一个刷新令牌
https://www.googleapis.com/oauth2/v3/token
参数
var params = {
code: 'FROM ANDROID [ONE TIME CODE]',
client_id: 'FROM YOUR GOOGLE CONSOLE',
client_secret: 'FROM YOUR GOOGLE CONSOLE',
redirect_uri: 'FROM YOUR GOOGLE CONSOLE',
grant_type: 'authorization_code'
access_type:'offline'
};
您必须从android发送的唯一内容是一次性代码。其他参数是静态的,请将它们存储在配置文件中
var params = {
code: 'FROM ANDROID [ONE TIME CODE]',
Android方面,在实现了这个之后,您将获得一个一次性代码
我希望这些将对您有所帮助。显示不起作用的代码可能会对您有所帮助