php 尝试刷新xero API令牌时获得“unsupported_grant_type”

krcsximq  于 2023-05-27  发布在  PHP
关注(0)|答案(2)|浏览(143)

我不知道如何使用离线访问。你能给我发个例子吗?我在哪里可以获得新的刷新令牌?
我为所有用户添加了offline_access。我该怎么用这个?用于生成新刷新令牌。
我有这个方法

$provider = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => $client,
'clientSecret' => $secret,
'redirectUri' => 'https://site/xeroreports',
'urlAuthorize' => 'https://login.xero.com/identity/connect/authorize',
'urlAccessToken' => 'https://identity.xero.com/connect/token',
'urlResourceOwnerDetails' => 'https://identity.xero.com/resources'
]);

$newAccessToken = $provider->getAccessToken('refresh_token', [
'refresh_token' => $xero_refresh
]);

但我只得到一个收据
如果我用

$client = new \GuzzleHttp\Client();
    
    $response1 = $client->request('POST', 'https://identity.xero.com/connect/token', [
    'body' => '{"grant_type":"refresh_token", "refresh_token":"'. $xero_refresh .'"}',
    'headers' => [
    'Authorization' => 'Basic ' . $authpas,
    'Content-Type' => "application/x-www-form-urlencoded",
    ],
    ]); 
echo $newRefreshToken = $response1->getBody();

我有个错误

Type: GuzzleHttp\Exception\ClientException Code: 400 Message: Client error: `POST https://identity.xero.com/connect/token` resulted in a `400 Bad Request` response: {error: unsupported_grant_type}
hmmo2u0o

hmmo2u0o1#

$newAccessToken = $provider->getAccessToken('refresh_token', [
 'refresh_token' => $xero_refresh
]);

在上面的代码片段中,$newAccessToken不是一个字符串,而是一个对象。要获得新的刷新令牌,只需$newAccessToken->getRefreshToken(),正如他们的wiki中提到的那样。

1tuwyuhd

1tuwyuhd2#

我参加聚会可能有点晚了,但我想我应该出点钱。
您将content-type设置为application/x-www-form-urlencoded,但主体作为JSON对象传递。
下面的代码应该可以工作:

$client = new \GuzzleHttp\Client();

$response1 = $client->request('POST', 'https://identity.xero.com/connect/token', [
'headers' => [
'Authorization' => 'Basic ' . $authpas,
'Content-Type' => "application/x-www-form-urlencoded"
],
'form_params' => [
    "grant_type" => "refresh_token",
    "client_id" => $client,
    "refresh_token" => $xero_refresh
],
]);

echo $newRefreshToken = $response1->getBody();

相关问题