使Google登录与应用程序的用户帐户匹配

时间:2020-04-07 20:43:08

标签: php google-api google-signin

我对此的集成是基于PHP的问题,尽管此问题也应适用于使用Google Sign-In的非PHP堆栈,但我将针对上下文进行解释。

我正在使用composer软件包League / oauth2-google将Google登录表单集成到许多用户的自定义PHP应用程序中:

https://github.com/thephpleague/oauth2-google

从用户向Google授予其凭据的应用程序的Google登录页面重定向后,我可以运行以下命令从已配置的Google API帐户获取此API信息:

$token = $this->provider->getAccessToken('authorization_code', [
    'code' => $_GET['code']
]);

// We got an access token, let's now get the owner details
$ownerDetails = $this->provider->getResourceOwner($token)->toArray();

dump($ownerDetails);

dump($token);

输出以下内容:

array:9 [▼
   "sub" => "12345678901234567890"
   "name" => "John Doe"
   "given_name" => "John"
   "family_name" => "Doe"
   "picture" => "https://lh3.googleusercontent.com/a-/example"
   "email" => "jdoe@example.com"
   "email_verified" => true
   "locale" => "en"
   "hd" => "example.com"
]

AccessToken {#215 ▼
    #accessToken: "example-token"
    #expires: 1586293521
    #refreshToken: null
    #resourceOwnerId: null
    #values: array:3 [▼
        "scope" => "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email openid"
        "token_type" => "Bearer"
        "id_token" => "example-token"
    ]
}

Google的文档说要使用子编号来标识用户。例如:

https://youtu.be/j_31hJtWjlw?t=60

我正在处理的应用程序使用用户名来标识用户。我们也有用户的电子邮件,但是不能保证它们都链接到用户的Google帐户。

当用户使用Google登录登录应用程序时,我应该如何将Google API中的信息与应用程序中的信息进行匹配?我想知道对此的典型流程是什么(如果有的话),或者至少了解处理此问题的好方法?

我们有大量的现有用户想要切换为使用基于Google的登录名。用户帐户中是否有一个可以获取子编号的地方,我可以允许他们将其粘贴到我的应用程序中的表单中,以授予这些用户通过Google登录的访问权限?

0 个答案:

没有答案