gpt4 book ai didi

php - 从 bitbucket 获取电子邮件

转载 作者:搜寻专家 更新时间:2023-10-31 21:30:48 25 4
gpt4 key购买 nike

我需要在 Symfony2 项目上实现 bitbucket 授权。我正在使用 HWIOAuthBundle 和 Guzzle 来形成请求。要获取电子邮件,我需要从中获取 json 响应 https://bitbucket.org/api/1.0/users/ {账户名}/电子邮件但是每次我得到

Client error response [url] https://bitbucket.org/api/1.0/users/{accountname}/emails [status code] 401 [reason phrase] UNAUTHORIZED"

但我已经在 Bitbucket 获得授权。这是我的代码:

$url = 'https://bitbucket.org/api/1.0/users/'.$response->getUsername().'/emails';
$client = new Client();
$request = $client->createRequest('GET', $url);
$emails = $client->send($request);

最佳答案

看起来您在发出请求时没有提供访问 token ,因此 Bitbucket 以 401 拒绝它们。

来自他们的 docs :

Once you have an access token, as per RFC-6750, you can use it in a request in any of the following ways (listed from most to least desirable):

  1. Send it in a request header: Authorization: Bearer {access_token}
  2. Include it in a (application/x-www-form-urlencoded) POST body as access_token={access_token}
  3. Put in the query string of a non-POST: ?access_token={access_token}

我会首先查找您正在使用的 OAuth 库的文档,并找出如何获取特定服务(在您的情况下为 Bitbucket)的用户访问 token 。然后,我会将其包含在 Guzzle 的请求 header 中:

$client = new Client();

$request = $client->createRequest(
'GET',
'https://bitbucket.org/api/1.0/users/' . $response->getUsername() . '/emails',
[
'Authorization' => 'Bearer ' . $userOauthAccessToken,
]
);

$emails = $client->send($request);

关于php - 从 bitbucket 获取电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30260851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com