2016-03-02 37 views
2

在Google Oauth2实现中,我试图使用guzzle调用来交换令牌的授权代码。如何使用查询字符串参数制作Guzzle文章

下狂饮调用工作正常,返回预期值:

$result = $this->client->post(
     'https://www.googleapis.com/oauth2/v3/token?code=<authorization_code>&redirect_uri=<redirect_uri>&client_id=<client_id>&client_secret=<client_secret>&grant_type=authorization_code') 
->getBody()->getContents(); 

但是这似乎是一个肮脏的方式来安装post请求。

我已尝试以下方法,以使它更清洁:

$result = $this->client->post(
     'https://www.googleapis.com/oauth2/v3/token', 
     [ 
      'query' => 
       [ 
        'code' => <authorization_code>, 
        'redirect_uri' => <redirect_uri>, 
        'client_id' => <client_id>, 
        'client_secret' => <client_secret> 
        'grant_type' => 'authorization_code', 
       ] 
     ] 
    )->getBody()->getContents(); 

然而,这第二次调用生成Malformed Json错误消息。

任何想法我可能做错了或如何调试上面的例子中生成的最终url是什么?

+1

我的第一个猜测是,发送的请求在某种程度上不完全相同。我将开始使用“调试”请求选项。传递一个真值作为您的客户实例或您的发布请求中的一个选项。 –

回答

3

我尝试没有code参数和它的工作。

$client = new \GuzzleHttp\Client(); 

$response = $client->post('https://www.googleapis.com/oauth2/v3/token', [ 
    'query' => [ 
     'client_id' => '...apps.googleusercontent.com', 
     'client_secret' => 'secret', 
     'refresh_token' => 'token', 
     'grant_type' => 'refresh_token' 
    ] 
]); 

$token = $response->getBody()->getContents() 
相关问题