Google api php 客户端代码不是 return 刷新令牌
Google api php client code not return refresh token
我一直在尝试使用 javascript google 客户端“代码”从 google api 获取刷新令牌 google return从客户端。它 return 是我发送到服务器端的代码。现在从服务器端我发送代码以使用 使用 google-api-php-client 获取刷新令牌和访问令牌通话:
https://www.googleapis.com/oauth2/v4/token
虽然我使用来自 google playground 的相同代码,但我也得到了带有刷新令牌的响应,但我没有从我自己的服务器获得它。
这是代码
public function getRefreshToken($code)
{
$client = new Google_Client();
$client->setClientId(config('services.google.client_id'));
$client->setClientSecret(config('services.google.client_secret'));
$client->setRedirectUri('postmessage');
$client->setScopes(config('services.google.scopes'));
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
dd($client->authenticate($code));
dd($client->getRefreshToken());
return ;
}
我已将访问类型设置为脱机,如某些答案中所述,但我仍然收到带有刷新令牌的响应。这就是响应
access_token :"xxxxxxxxxxxxxxxxxxxxxx"
created:1510242052
expires_in:3598
id_token:"xxxxxxxx"
token_type:"Bearer"
我觉得你的 php 代码没问题。我怀疑您的前端 javascript,特别是它构建的 link 到 google,可能是罪魁祸首。 authorization_code 是否在兑换时产生刷新令牌部分取决于初始 link 到 Google.
中包含的参数
如 this answer 中所述,javascript 客户端库使用 "client side flow"。通常在前端应用程序中,您会指定 response_type=token
,但如果您指定 response_type=code
,则会返回 code
。但是在兑换时,code
不会产生刷新令牌。
例如,a link built by a front end javascript library 可能如下所示:
https://accounts.google.com/o/oauth2/v2/auth?client_id=7xxxxxxxxxxx-xxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=http://localhost:8080/oauth2callback.php&response_type=code&scope=profile
您的后端可以兑换返回的 code
,但响应不会包含刷新令牌。这是设计使然。
获得符合刷新令牌条件的 code
的一种方法是使用后端 PHP 客户端库构建 link,而不是 javascript 客户端库。 $client->createAuthUrl()
将像这样构建一个 link:
https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=离线&client_id=7xxxxxxxxxx-hxxxxxxxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=http% 3A%2F%2Flocalhost%3A8080%2Foauth2callback.php&state&scope=profile&approval_prompt=force
This toy example 以这种方式构建 link 并接收刷新令牌。
注意 access_type=offine
和 approval_prompt=force
的添加。身份验证成功后,本例中的重定向包含一个 code
, 在兑换时 提供刷新令牌。
OAuth 2.0 游乐场构建了一个包含 access_type=offline
和 prompt=consent
的初始 link,它还创建了一个可兑换刷新令牌的代码。
如果这没有帮助,也许您可以将您的前端正在构建的 link 更新为 google 的问题? (当然,客户 ID 已经过编辑)
我一直在尝试使用 javascript google 客户端“代码”从 google api 获取刷新令牌 google return从客户端。它 return 是我发送到服务器端的代码。现在从服务器端我发送代码以使用 使用 google-api-php-client 获取刷新令牌和访问令牌通话:
https://www.googleapis.com/oauth2/v4/token
虽然我使用来自 google playground 的相同代码,但我也得到了带有刷新令牌的响应,但我没有从我自己的服务器获得它。 这是代码
public function getRefreshToken($code)
{
$client = new Google_Client();
$client->setClientId(config('services.google.client_id'));
$client->setClientSecret(config('services.google.client_secret'));
$client->setRedirectUri('postmessage');
$client->setScopes(config('services.google.scopes'));
$client->setAccessType("offline");
$client->setApprovalPrompt("force");
dd($client->authenticate($code));
dd($client->getRefreshToken());
return ;
}
我已将访问类型设置为脱机,如某些答案中所述,但我仍然收到带有刷新令牌的响应。这就是响应
access_token :"xxxxxxxxxxxxxxxxxxxxxx"
created:1510242052
expires_in:3598
id_token:"xxxxxxxx"
token_type:"Bearer"
我觉得你的 php 代码没问题。我怀疑您的前端 javascript,特别是它构建的 link 到 google,可能是罪魁祸首。 authorization_code 是否在兑换时产生刷新令牌部分取决于初始 link 到 Google.
中包含的参数如 this answer 中所述,javascript 客户端库使用 "client side flow"。通常在前端应用程序中,您会指定 response_type=token
,但如果您指定 response_type=code
,则会返回 code
。但是在兑换时,code
不会产生刷新令牌。
例如,a link built by a front end javascript library 可能如下所示:
https://accounts.google.com/o/oauth2/v2/auth?client_id=7xxxxxxxxxxx-xxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=http://localhost:8080/oauth2callback.php&response_type=code&scope=profile
您的后端可以兑换返回的 code
,但响应不会包含刷新令牌。这是设计使然。
获得符合刷新令牌条件的 code
的一种方法是使用后端 PHP 客户端库构建 link,而不是 javascript 客户端库。 $client->createAuthUrl()
将像这样构建一个 link:
https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=离线&client_id=7xxxxxxxxxx-hxxxxxxxxxxxxxxxx.apps.googleusercontent.com&redirect_uri=http% 3A%2F%2Flocalhost%3A8080%2Foauth2callback.php&state&scope=profile&approval_prompt=force
This toy example 以这种方式构建 link 并接收刷新令牌。
注意 access_type=offine
和 approval_prompt=force
的添加。身份验证成功后,本例中的重定向包含一个 code
, 在兑换时 提供刷新令牌。
OAuth 2.0 游乐场构建了一个包含 access_type=offline
和 prompt=consent
的初始 link,它还创建了一个可兑换刷新令牌的代码。
如果这没有帮助,也许您可以将您的前端正在构建的 link 更新为 google 的问题? (当然,客户 ID 已经过编辑)