如何将参数添加到 google oauth 2
How to add a param to google oauth 2
我正在尝试使用以下代码向 google oAuth 发出授权请求
$client = new Google_Client();
$client->setClientId('qweqweqweqwe');
$client->setClientSecret('vQWsd1Geweqweqweqwe');
$client->setRedirectUri('http://localhost/test1');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setScopes(['https://www.googleapis.com/auth/gmail.readonly']);
if (request()->has('code')) {
$credentials = $client->authenticate(request('code'));
dd($credentials);
}
它的工作,但我的问题是:有没有办法在请求中添加用户 ID 并在回调时取回它?
您可以使用 state
与 Google_Client::setState()
一起发送自定义参数。
设置客户端时:
$client = new Google_Client();
$client->setClientId('qweqweqweqwe');
$client->setClientSecret('vQWsd1Geweqweqweqwe');
$client->setRedirectUri('http://localhost/test1');
$client->setState('test=value');
并且在回调时,参数将在 state
中通过 GET:
var_dump($_GET["state"]); // test=value
您几乎可以发送任何您想要发送的内容,因此如果数据变得太复杂,请尝试 json_encode()
或 url_encode()
处理您的数据。
添加该参数的一个很好的观点也可以防止 csrf 攻击。
在 google 授权请求上添加参数,然后在 google 的回调请求上取回参数的方式是设置一个 base64
编码的 json有效负载:
在生成授权之前 url 首先包含两个函数来编码和解码 url 参数,简单:
public function base64UrlEncode($inputStr)
{
return strtr(base64_encode($inputStr), '+/=', '-_,');
}
public function base64UrlDecode($inputStr)
{
return base64_decode(strtr($inputStr, '-_,', '+/='));
}
向 google 发出 oauth 请求,使 url 参数示例:
$params = base64UrlEncode('{ "a" : "b" , "c" : 1 }');
在 Google 客户端 class 中有一个函数 setState($state)
,您需要在创建 url 之前调用该函数并将 $params
作为参数传递,例如:
$client = new Google_Client();
$client->setClientId('qweqweqweqwe');
$client->setClientSecret('vQWsd1Geweqweqweqwe');
$params = base64UrlEncode('{ "a" : "b" , "c" : 1 }');
$client->setState($params);
$client->setRedirectUri('http://localhost/test1');
然后响应将具有状态请求参数,因此在回调路由中执行:
Route::get('/callback', function(){
$state = request()->get('state'); // GET['state'];
$state = base64_decode(strtr($state, '-_,', '+/='));
dd($state); // will output your params
});
这个答案有点基于:
here
我正在尝试使用以下代码向 google oAuth 发出授权请求
$client = new Google_Client();
$client->setClientId('qweqweqweqwe');
$client->setClientSecret('vQWsd1Geweqweqweqwe');
$client->setRedirectUri('http://localhost/test1');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setScopes(['https://www.googleapis.com/auth/gmail.readonly']);
if (request()->has('code')) {
$credentials = $client->authenticate(request('code'));
dd($credentials);
}
它的工作,但我的问题是:有没有办法在请求中添加用户 ID 并在回调时取回它?
您可以使用 state
与 Google_Client::setState()
一起发送自定义参数。
设置客户端时:
$client = new Google_Client();
$client->setClientId('qweqweqweqwe');
$client->setClientSecret('vQWsd1Geweqweqweqwe');
$client->setRedirectUri('http://localhost/test1');
$client->setState('test=value');
并且在回调时,参数将在 state
中通过 GET:
var_dump($_GET["state"]); // test=value
您几乎可以发送任何您想要发送的内容,因此如果数据变得太复杂,请尝试 json_encode()
或 url_encode()
处理您的数据。
添加该参数的一个很好的观点也可以防止 csrf 攻击。
在 google 授权请求上添加参数,然后在 google 的回调请求上取回参数的方式是设置一个 base64
编码的 json有效负载:
在生成授权之前 url 首先包含两个函数来编码和解码 url 参数,简单:
public function base64UrlEncode($inputStr)
{
return strtr(base64_encode($inputStr), '+/=', '-_,');
}
public function base64UrlDecode($inputStr)
{
return base64_decode(strtr($inputStr, '-_,', '+/='));
}
向 google 发出 oauth 请求,使 url 参数示例:
$params = base64UrlEncode('{ "a" : "b" , "c" : 1 }');
在 Google 客户端 class 中有一个函数 setState($state)
,您需要在创建 url 之前调用该函数并将 $params
作为参数传递,例如:
$client = new Google_Client();
$client->setClientId('qweqweqweqwe');
$client->setClientSecret('vQWsd1Geweqweqweqwe');
$params = base64UrlEncode('{ "a" : "b" , "c" : 1 }');
$client->setState($params);
$client->setRedirectUri('http://localhost/test1');
然后响应将具有状态请求参数,因此在回调路由中执行:
Route::get('/callback', function(){
$state = request()->get('state'); // GET['state'];
$state = base64_decode(strtr($state, '-_,', '+/='));
dd($state); // will output your params
});
这个答案有点基于: here