Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RE: BitbucketResourceOwner::getEmail #6 #9

Open
therockers opened this issue Jun 1, 2019 · 1 comment
Open

RE: BitbucketResourceOwner::getEmail #6 #9

therockers opened this issue Jun 1, 2019 · 1 comment

Comments

@therockers
Copy link

Just going to post this here in case the comment I made wasn't visible due to the thread being closed.

#6 (comment)

@therockers
Copy link
Author

Actually. As soon as I posted the code in that comment, the day after that, BitBucket changed their API version from 1.0 to 2.0. In version 2.0, the URL is different and doesn't require the username. Just only needs the access token. Here is an updated code snippet to comply with version 2.0.

$curl = curl_init();

curl_setopt_array($curl, [
	CURLOPT_RETURNTRANSFER => 1,
	CURLOPT_URL => 'https://bitbucket.org/api/2.0/user/emails?access_token=' . $token->getToken(),
	CURLOPT_HTTPHEADER, [
		'Authorization' => 'Bearer ' . $token->getToken()
	]
]);

$result = curl_exec($curl);

curl_close($curl);

$decode = json_decode($result);

print_r($decode);

Assuming you're also using the code snippet from the example code in the README.md file. The end result will now actually look like this.

stdClass Object
(
    [pagelen] => 10
    [values] => Array
        (
            [0] => stdClass Object
                (
                    [is_primary] => 1
                    [is_confirmed] => 1
                    [type] => email
                    [email] => [email protected]
                    [links] => stdClass Object
                        (
                            [self] => stdClass Object
                                (
                                    [href] => https://bitbucket.org/!api/2.0/user/emails/[email protected]
                                )

                        )

                )

        )

    [page] => 1
    [size] => 1
)

[email protected] is a dummy to demonstrate what the cURL request sends back.

To access this object and get the email address, you can then just do this.

$decode->values[0]->email

There's nothing too complicated about that. Hopefully, this code snippet could be added to this repo's live code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant