1

我已经在简化的身份流程中实现了帐户链接(https://developers.google.com/actions/identity/oauth2-assertion-flow

// Get the jwt token from the request
    $token = $this->jwt->parse($body['assertion']);

    if (null === Client::where('id', $body['client_id'])->where('secret', $body['client_secret'])->first()
        && (false === $this->verifySignature($token) || false === $this->validateData($token))
    ) {
        return response()->json(
            [
                'error' => 'invalid_grant',
            ],
            Http::UNAUTHORIZED
        );
    }

    $sub = $token->getClaim('sub');
    $email = $token->getClaim('email');
    $scopes = explode(' ', $body['scope']);

    // Check if the user exists in the database or not
    $userId = User::findIdByGoogleIds($email, $sub);
    \Log::debug($userId);

    // If user doesn't exists and intent is get, ask google to send you the create intent with all user info.
    if (null === $userId && 'get' === $body['intent']) {
        \Log::debug('user not found, ask for a create request');

        return response()->json(
            [
                'error' => 'user_not_found',
            ],
            Http::UNAUTHORIZED
        );

    // If user doesn't exist and we are in intent create, let's create this user in our database.
    } elseif (null === $userId && 'create' === $body['intent']) {
        \Log::debug('user not found, create it');

        // If user exists bug don't have the google profile associated.
        if (null === ($user = User::findByMail($email))) {
            $user = new User();
            $user->password = Hash::make(str_random(8));
            $user->email = $email;
            $user->name = $token->getClaim('name');

            $user->save();
            //Generate a token for the user
            static::generateToken($user, $scopes);
        }

        // Asssociate google profile that user
        $profile = new GoogleProfile();
        $profile->google_id = $sub;
        $user->googleProfiles()->save($profile);

    } elseif (null !== $userId) {
        \Log::debug('google profile already existing');

        $user = User::find($userId->id);
    }

JWT 令牌包含这​​些信息:

{
  "sub": 1234567890,        // The unique ID of the user's Google Account
  "iss": "https://accounts.google.com",        // The assertion's issuer
  "aud": "123-abc.apps.googleusercontent.com", // Your server's client ID
  "iat": 233366400,         // Unix timestamp of the assertion's creation time
  "exp": 233370000,         // Unix timestamp of the assertion's expiration time
  "name": "Jan Jansen",
  "given_name": "Jan",
  "family_name": "Jansen",
  "email": "jan@gmail.com", // If present, the user's email address
  "locale": "en_US"
}

我需要获取用户在他的谷歌个人资料中填写的电话号码(如果有的话)。

所以我想使用google + api,所以我正在尝试这个:

    $client = new \Google_Client();
    $client->setClientId('my_client_id');
    $client->setClientSecret('my_client_secret');
    $client->setAccessToken($token->getPayload());
    $plus = new Google_Service_Plus($client);

    $test = $plus->people->get($sub);

client_id 和 secret 已在https://console.developers.google.com上创建

回应是:

 {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}
 {"exception":"[object] (Google_Service_Exception(code: 401): {
 \"error\": {
  \"errors\": [
   {
    \"domain\": \"global\",
    \"reason\": \"authError\",
    \"message\": \"Invalid Credentials\",
    \"locationType\": \"header\",
    \"location\": \"Authorization\"
   }
  ],
  \"code\": 401,
  \"message\": \"Invalid Credentials\"
 }
}

因此,据我了解,帐户链接是 OAuth 身份验证,因此它应该向我发送一个有效的令牌访问权限,我可以将其与 google api 一起使用。但它告诉我这个令牌是无效的。

我错过了什么或者我没有以好的方式做到这一点?我是否必须再次执行特定于 google api 的新身份验证?

4

1 回答 1

3

你如何做你想做的事情有很多问题。

首先,您获得的令牌assertion是身份令牌,而不是授权令牌。令牌本身不是授权您代表用户执行任何操作的不记名令牌。您指向的关于断言流程的指南说您应该验证令牌,然后使用它从sub字段中获取用户的唯一 ID。您应该使用此 ID 来确定用户是否已经在您的系统中进行了身份验证,并使用您为用户提供的 Auth Token 和 Refresh Token。

其次,您可能不需要 Google+ API,因为Google+ People 对象不包含电话号码字段。如果是这样,并且如果您想使用“me”参数获取有关用户的私人信息,则需要在用户登录时请求 thehttps://www.googleapis.com/auth/plus.loginhttps://www.googleapis.com/auth/plus.meauth 范围。

相反,您可能想要使用People APIpeople 对象包含您可以使用get方法检索的电话号码,但有一些限制:

  • 他们实际上必须设置电话号码,并且您需要将该字段请求为要获取的字段之一
  • 如果他们公开了,你应该可以得到它。
  • 如果他们没有公开,您需要申请https://www.googleapis.com/auth/user.phonenumbers.read范围。
于 2018-06-25T19:04:51.353 回答