Android GoogleAuthUtil:获取将提供电子邮件地址的访问令牌?

Android GoogleAuthUtil:获取将提供电子邮件地址的访问令牌?,android,oauth,oauth-2.0,google-play,google-play-services,Android,Oauth,Oauth 2.0,Google Play,Google Play Services,我无法获取访问令牌以提供电子邮件地址。我在用GoogleAuthUtil。(结果表明令牌无效) 我用这个: mGoogleApiClient = new GoogleApiClient.Builder (cordova.getActivity().getApplicationContext()) .addConnectionCallbacks (this) .addOnConnectionFailedListener (this) .addApi (Games.API)

我无法获取访问令牌以提供电子邮件地址。我在用GoogleAuthUtil。(结果表明令牌无效)

我用这个:

  mGoogleApiClient = new GoogleApiClient.Builder (cordova.getActivity().getApplicationContext())
   .addConnectionCallbacks (this)
   .addOnConnectionFailedListener (this)
   .addApi (Games.API)
   .addScope (Games.SCOPE_GAMES)
   .addApi(Plus.API)
   .addScope(Plus.SCOPE_PLUS_PROFILE)
   .addScope(Plus.SCOPE_PLUS_LOGIN)
   .build ();

这是:

scope=“oauth2:“+Scopes.PROFILE++”“电子邮件”


删除“email”可以给我一个有效的访问令牌——但是响应中没有电子邮件地址

这是我的密码

package com.flyingsoftgames.googleplayservices;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.games.Players;
import com.google.android.gms.games.Games;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
import com.google.android.gms.plus.Account;
import com.google.android.gms.plus.Plus;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.PluginResult.Status;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;

import android.app.Activity;

import android.os.AsyncTask;
import android.os.Bundle;
import org.apache.cordova.*;

import java.io.IOException;

import android.util.Log;

public class GooglePlayServices extends CordovaPlugin implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

 private static final String LOG_TAG = "GooglePlayServices";
 private static final int REQ_SIGN_IN_REQUIRED = 55664;

 public static GoogleApiClient mGoogleApiClient   = null;
 public CallbackContext        tryConnectCallback = null;
 public String                 accessToken        = "";
 private int                   connectionAttempts = 0;
 @Override public void onConnectionFailed (ConnectionResult result) {
  String errormessage = result.toString();
  Log.w (LOG_TAG, errormessage);
  connectionAttempts += 1;
  if (!result.hasResolution() || connectionAttempts >= 2) {
   Log.w (LOG_TAG, "Error: no resolution. Google Play Services connection failed.");
   tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;
   return;
  }
  try {
   result.startResolutionForResult (cordova.getActivity(), result.getErrorCode());
  } catch (SendIntentException e) {
   // There was an error with the resolution intent. Try again.
   mGoogleApiClient.connect ();
  }
 }

 @Override public void onConnected (Bundle connectionHint) {
  String mAccountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
  new RetrieveTokenTask().execute (mAccountName);
  Games.setViewForPopups (mGoogleApiClient, webView);
 }

 public void onActivityResult (int requestCode, int responseCode, Intent intent) {
  if (!mGoogleApiClient.isConnecting()) mGoogleApiClient.connect ();
 }

 @Override public void onConnectionSuspended (int cause) {
  mGoogleApiClient.connect ();
 }

 public boolean execute (String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {
  if        ("getPlayerId".equals(action)) {
   String playerId = Games.Players.getCurrentPlayerId (mGoogleApiClient);
   callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, playerId));
  } else if ("tryConnect".equals(action)) {
   tryConnect (callbackContext);
  } else if ("getAccessToken".equals(action)) {
   callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, accessToken));
  }
  return true;
 }

 // tryConnect runs the callback with a value of false if Google Play Services isn't available.
 public void tryConnect (CallbackContext callbackContext) {
  boolean isGpsAvailable = (GooglePlayServicesUtil.isGooglePlayServicesAvailable(cordova.getActivity()) == ConnectionResult.SUCCESS);
  if (!isGpsAvailable) {
   callbackContext.sendPluginResult (new PluginResult (PluginResult.Status.OK, false));
   return;
  }
  tryConnectCallback = callbackContext;
  mGoogleApiClient = new GoogleApiClient.Builder (cordova.getActivity().getApplicationContext())
   .addConnectionCallbacks (this)
   .addOnConnectionFailedListener (this)
   .addApi (Games.API)
   .addScope (Games.SCOPE_GAMES)
   .addApi(Plus.API)
   .addScope(Plus.SCOPE_PLUS_PROFILE)
   .addScope(Plus.SCOPE_PLUS_LOGIN)
   .build ();
  mGoogleApiClient.connect ();
 }


 private class RetrieveTokenTask extends AsyncTask<String, Void, String> {
  @Override protected String doInBackground (String... params) {
   String accountName = params[0];
   String scope = "oauth2:" + Scopes.PROFILE + " " + "email";
   Context context = cordova.getActivity().getApplicationContext();
   try {
    accessToken = GoogleAuthUtil.getToken (context, accountName, scope);
   } catch (IOException e) {
    String errormessage = e.getMessage();
    Log.e (LOG_TAG, errormessage);
    if (tryConnectCallback != null) tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;
   } catch (UserRecoverableAuthException e) {
    cordova.getActivity().startActivityForResult (e.getIntent(), REQ_SIGN_IN_REQUIRED);
   } catch (GoogleAuthException e) {
    String errormessage = e.getMessage();
    Log.e (LOG_TAG, errormessage);
    if (tryConnectCallback != null) tryConnectCallback.error ("Error: " + errormessage + "."); tryConnectCallback = null;
   }
   return accessToken;
  }

  @Override protected void onPostExecute (String newAccessToken) {
   super.onPostExecute (newAccessToken);
   accessToken = newAccessToken;   
   if (tryConnectCallback != null) {
    String playerId = Games.Players.getCurrentPlayerId (mGoogleApiClient);
    tryConnectCallback.sendPluginResult (new PluginResult (PluginResult.Status.OK, playerId));
    tryConnectCallback = null;
   }
  }
 }
}
package com.flyingsoftgames.googleplayservices;
导入com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.Scopes;
导入com.google.android.gms.common.api.GoogleAppClient;
导入com.google.android.gms.common.api.GoogleAppClient.ConnectionCallbacks;
导入com.google.android.gms.common.api.GoogleAppClient.OnConnectionFailedListener;
导入com.google.android.gms.common.GooglePlayServicesUtil;
导入com.google.android.gms.games.Players;
导入com.google.android.gms.games.games;
导入com.google.android.gms.auth.GoogleAuthException;
导入com.google.android.gms.auth.GoogleAuthUtil;
导入com.google.android.gms.auth.UserRecoverableAuthException;
导入com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
导入com.google.android.gms.plus.Account;
导入com.google.android.gms.plus.plus;
导入org.apache.cordova.CallbackContext;
导入org.apache.cordova.cordova接口;
导入org.apache.cordova.CordovaWebView;
导入org.apache.cordova.CordovaPlugin;
导入org.apache.cordova.PluginResult;
导入org.apache.cordova.PluginResult.Status;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentSender.SendIntentException;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入org.apache.cordova.*;
导入java.io.IOException;
导入android.util.Log;
公共类GooglePlayServices扩展CordovaPlugin实现GoogleAppClient.ConnectionCallbacks、GoogleAppClient.OnConnectionFailedListener{
私有静态最终字符串日志\u TAG=“GooglePlayServices”;
专用静态最终整数要求符号要求=55664;
公共静态GoogleAppClient MgoogleAppClient=null;
public CallbackContext tryConnectCallback=null;
公共字符串accessToken=“”;
私有int-connectionAttents=0;
@覆盖onConnectionFailed(ConnectionResult)上的公共void{
String errormessage=result.toString();
Log.w(日志标签,错误消息);
ConnectionAttents+=1;
如果(!result.hasResolution()| | connectionAttents>=2){
w(Log_标签,“错误:无解析。谷歌播放服务连接失败”);
tryConnectCallback.error(“错误:+errormessage+”);tryConnectCallback=null;
返回;
}
试一试{
result.startResolutionForResult(cordova.getActivity(),result.getErrorCode());
}捕获(发送){
//解析意图出错。请重试。
mGoogleApiClient.connect();
}
}
@覆盖已连接的公共void(Bundle connectionHint){
字符串mAccountName=Plus.AccountApi.getAccountName(mGoogleApiClient);
新建RetrieveTokenTask().execute(mAccountName);
Games.setViewForPopups(mgoogleapclient、webView);
}
ActivityResult上的公共无效(int请求代码、int响应代码、意图){
如果(!mgoogleapclient.isConnecting())mgoogleapclient.connect();
}
@覆盖连接已暂停的公共无效(内部原因){
mGoogleApiClient.connect();
}
公共布尔执行(字符串操作、JSONArray输入、CallbackContext CallbackContext)抛出JSONException{
如果(“getPlayerId.equals(action)){
字符串playerId=Games.Players.getCurrentPlayerId(mGoogleApiClient);
callbackContext.sendPluginResult(新的PluginResult(PluginResult.Status.OK,playerId));
}else if(“tryConnect”.equals(action)){
tryConnect(callbackContext);
}else if(“getAccessToken.equals(action)){
callbackContext.sendPluginResult(新的PluginResult(PluginResult.Status.OK,accessToken));
}
返回true;
}
//如果googleplay服务不可用,tryConnect将以false值运行回调。
public void tryConnect(CallbackContext CallbackContext){
布尔值isgpsavaailable=(GooglePlayServicesUtil.isgoogleplayservicesavaailable(cordova.getActivity())==ConnectionResult.SUCCESS);
如果(!isgpsavaailable){
callbackContext.sendPluginResult(新的PluginResult(PluginResult.Status.OK,false));
返回;
}
tryConnectCallback=callbackContext;
mGoogleApiClient=新的GoogleApiClient.Builder(cordova.getActivity().getApplicationContext())
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.addApi(Games.API)
.addScope(Games.SCOPE_Games)
.addApi(加上.API)
.addScope(Plus.SCOPE\u Plus\u配置文件)
.addScope(Plus.SCOPE\u Plus\u登录)
.build();
mGoogleApiClient.connect();
}
私有类RetrieveTokenTask扩展异步任务{
@覆盖受保护的字符串doInBackground(字符串…参数){
字符串accountName=params[0];
String scope=“oauth2:“+Scopes.PROFILE+”“+”电子邮件”;
Context Context=cordova.getActivity().getApplicationContext();
试一试{
accessToken=GoogleAuthUtil.getToken(上下文、帐户名、范围);
}捕获(IOE异常){
字符串errormessage=e.getMessage();
Log.e(日志标签,错误消息);
如果(tryConnectCallback!=null)tryConnectCallback.error(“错误:+errormessage+”);tryConnectCallback=null;
}捕获(UserRecoverableAuthe异常){
cordova.getActivity().startActivityForResult(例如getIntent(),需要签名);
}捕获(googleauthe异常){
字符串errormessage=e.getMessage();
Log.e(日志标签,错误消息);
如果(tryConnectCallback!=null)tryConnectCallback.error(“错误:+errormessage+”);tryConnectCallback=null;
}
返回accessToken;
}
@覆盖受保护的空
accessToken = GoogleAuthUtil.getToken(context, accountName, scope);
GoogleAuthUtil.clearToken (context, accessToken);
accessToken = GoogleAuthUtil.getToken(context, accountName, scope);