我正在创建一个基本的Android应用程序,允许用户使用谷歌登录并获取访问令牌。然后我将访问令牌发送到运行在php上的服务器,并使用来自谷歌的令牌获取用户信息。 下面是我在Android上获取访问令牌的代码
public static void init(Context context, AppCompatActivity activity, GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener){
if(googleSignInOptions == null) {
googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).
requestIdToken(context.getString(R.string.google_web_api_key)).
requestScopes(new Scope(Scopes.PLUS_ME)).
requestProfile().
requestEmail().build();
}
if(googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(context).enableAutoManage(activity, onConnectionFailedListener).
addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions).build();
}
}
//After login i call postGoogleSignin from onActivityResult
public static void postGoogleSignin(Watchable watchable, Intent data){
GoogleSignInResult googleSignInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if(googleSignInResult.isSuccess()){
Log.v(Constant.TAG, "Google Login is successful");
GoogleSignInAccount googleSignInAccount = googleSignInResult.getSignInAccount();
String token = googleSignInAccount.getIdToken();// Access Token obtained
}
else{
Log.e(Constant.TAG,"Error occured");
}
我将此令牌发送到我的服务器并尝试获取用户信息。 获取用户信息的代码
$gClient = new Google_Client();
$gClient->setApplicationName('pixyfi');
$gClient->setClientId($config['google']['appid']);
$gClient->setClientSecret($config['google']['secret']);
$privateKey=file_get_contents('googleAuth.json');
$privateKeyPassword='notasecret';
$scopes= array(
'https://www.googleapis.com/auth/plus.login'
);
$token = json_encode(array("access_token"=>$lptoken,"refresh_token"=>$lptoken,"token_type"=>"Bearer","expires_in"=>3600,"id_token"=>$lptoken,"created"=>time()));
$gClient->setAccessToken($token);
$google_oauthV2 = new Google_Service_Plus($gClient);
$gpUserProfile = $google_oauthV2->people->get('me'); //I get error here
echo "User Fetched as:".$gpUserProfile['id'];
你可以告诉我我到底错在哪里吗?
我已经按照facebook的相同程序进行操作,但是我收到了无效的凭据错误。