0

我想产生服务器权威性代码安卓登录谷歌服务器

gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
      .requestEmail() 
      .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER)) 
      .requestServerAuthCode(getString(R.string.server_client_id), false) 
      .build(); 

然后我试图让这样的服务器授权的代码。

result.requestServerAuthCode(getString(R.string.server_client_id), false) 

假设我得到了像'bla bla bla'这样的认证标志;

然后使用laravel社交名媛,我试图让用户在服务器端

Socialize::driver('google')->userFromToken('bla bla bla') 

它显示了我错误

GuzzleHttp\Exception\ClientException with message 'Client error: GET https://www.googleapis.com/plus/v1/people/me?prettyPrint=false resulted in a 401 Unauthorized` response: {"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","loc (truncated...)

+0

使用该授权码来获得访问令牌,见https://developers.google.com/identity/protocols/OAuth2,然后使用访问该端点的令牌 – BNK

+0

Thankyou进行回复!我正在网上寻找。你可以告诉我如何从php获取access_token或简单地卷曲@BNK –

+0

对不起,我不熟悉PHP和卷曲,但是,我认为你可以在https://github.com/google/google-api-php找到更多信息。客户端和http://stackoverflow.com/questions/28390718/not-able-to-fetch-google-oauth-2-0-access-token – BNK

回答

1

实际上是由谷歌在Android上发送的代码不是访问令牌获取访问令牌,您可以在laravel控制器上执行此操作。

安装此作曲家库https://github.com/pulkitjalan/google-apiclient

$client = new \PulkitJalan\Google\Client(['client_id' => 'YOUR_CLIENT_ID', 'client_secret' => 'YOUR_SECRET', 'redirect_uri' => 'YOUR_REDIRECT_URI', 'developer_key' => 'YOUR_KEY']); 
$google = $client->getClient(); 
$google->authenticate($token); 
$access_token = $client->getAccessToken()["access_token"]; 

//and now here you go 
$user = Socialize::driver('google')->userFromToken($access_token); 
+0

酷这真棒 – user3470929

0

最简单的方法是使用由谷歌提供的Google_Client类。你可以找到它here

现在,你有两个选择:

  1. 您可以发送id_token到服务器,并验证它只是为纽带说,然后就获取用户信息来自收到的对象。
  2. 您可以将backend_auth_code发送到服务器,而不是id_token,然后使用方法fetchAccessTokenWithAuthCode($代码)的Google_Client类。这给你访问令牌,ID令牌和其他东西。然后,您可以使用的access_token与Laravel社会名流

    $client = new Google_Client([ 
        'client_id' => config('services.google.client_id'), 
        'client_secret' => config('services.google.client_secret') 
    ]); 
    
    $data = $client->fetchAccessTokenWithAuthCode($code); 
    
    $user = Socialite::driver('google')->scopes(['profile','email'])->userFromToken($data['access_token']);