2017-08-13 93 views
0

我已按照these instructions在我的後端服務器上使用Android登錄。google/apiclient:簽名驗證失敗

Android的登錄作品,我發現了一個ID令牌與我的服務器的客戶端ID:

// Configure sign-in to request the user's ID, email address, and basic 
// profile. ID and basic profile are included in DEFAULT_SIGN_IN. 
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
     .requestIdToken(getString(R.string.server_client_id)) 
     .build(); 

// Build a GoogleApiClient with access to the Google Sign-In API and the 
// options specified by gso. 
mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
     .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
     .build(); 

mGoogleSignIn.setSize(SignInButton.SIZE_STANDARD); 
mGoogleSignIn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
     startActivityForResult(signInIntent, REQUEST_CODE_SIGN_IN); 
    } 
}); 

此令牌發送到我的後端服務器作爲POST請求(代碼無關:它作品)。

然後,服務器(一個symfony的後端)使用google/apiclient到再次認證所述用戶,以便驗證用戶ID和在後端數據庫獲取的用戶信息:

// Get token from user 
$id_token = $request->get('id_token'); 

// Validate token 
try { 
    $options = [ 
     'client_id' => $this->container->getParameter('google_app_id') 
    ]; 
    $client = new \Google_Client($options); 
    $payload = $client->verifyIdToken($id_token); 
    if ($payload) { 
     $user = $this->getDoctrine() 
        ->getRepository(User::class) 
        ->findOneBy(['googleId' => $payload['sub']]); 
     if ($user != null) { 
      $data = [ 
       'client_id' => $user->getClient()->getClientId(), 
       'client_secret' => $user->getClient()->getSecret(), 
      ]; 
     } else { 
      $data = [ 
       'error' => 'Unknown user', 
       'message' => 'Please create an account first', 
      ]; 
     } 
    } else { 
     $data = [ 
      'error' => 'Invalid token', 
     ]; 
    } 
} catch (\Exception $e) { 
    $data = [ 
     'error' => 'Invalid token', 
     'details' => $e->getMessage(), 
    ]; 
} 

異常上升,該消息是Signature Verification Failed

經過一些在線研究,我找不到此消息的原因。這是什麼造成的?在後端使用相同的代碼工作,我只是改變了Android的一些東西,但與Google登錄無關。

+1

難以分辨您提供的信息,您可以做一些事情,如更新OpenSSL並檢查是否配置時區 – Vamsi

+0

我知道我沒有提供任何信息。 OpenSSL和日期是正確的,但這是一個非常好的主意。 –

回答

0

今天早上我再次嘗試使用完全相同的代碼,它的工作就像一個魅力。我想這是Google方面的暫時失敗(沒想到!)。

我想了解更多的錯誤信息,而不是我的代碼中的錯誤(沒有任何!),所以如果有人有一個更清晰的解釋我會接受他們的答案。