Get an email from bitbucket

I need to implement bitbucket authorization in a Symfony2 project. I am using HWIOAuthBundle and Guzzle to form a request. To receive emails I need to get json response from https://bitbucket.org/api/1.0/users/ {accountname} / emails But every time I get

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

But I have already logged into Bitbucket. Here is my code:

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

      

+3


source to share


1 answer


You don't seem to provide an access token when making requests, so Bitbucket rejects them with a 401.

From the docs :

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

  • Send it in the request header: Authorization: Bearer {access_token}
  • Include it in POST body (application / x-www-form-urlencoded) as access_token = {access_token}
  • Insert a non-POST query string :? access_token = {access_token}


I will first go through the OAuth library docs that you are using and find out how to get a user's access token to a specific service (Bitbucket in your case). Then I would include it in the request headers in Guzzle:

$client = new Client();

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

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

      

+1


source







All Articles