Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Bitbucket API calls with access token?

I created an ASP.NET MVC application which can authorize the user at Bitbucket. I used CSharp.Bitbucket library to get the token secret and token value.

The OAuth tutorial said that with the token I can make API calls.

I know that I can call the API using basic authorization like this way:

 string url = "https://bitbucket.org/api/1.0/user/";
 var request = WebRequest.Create(url) as HttpWebRequest;

 string credentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("username" + ":" + "password"));
 request.Headers.Add("Authorization", "Basic " + credentials);

 using (var response = request.GetResponse() as HttpWebResponse)
 {
    var reader = new StreamReader(response.GetResponseStream());
    string json = reader.ReadToEnd();
 }

But how can I call the API using the access token?

Thank you very much!

like image 968
Gábor Domonkos Avatar asked Jul 28 '14 15:07

Gábor Domonkos


People also ask

How do I use API token in Bitbucket?

Create HTTP access tokens To create an HTTP access token for your user account: Go to Profile picture > Manage account > HTTP access tokens. Select Create token. Set the token name, permissions, and expiry.

How do I call API access token?

You need to perform the following: Register your app in the Security Token Service, based on IdentityServer3. Within your app, acquire an access token from the STS. Add an authorization header Bearer access_token and call the Sitefinity Web API.

How do I enable OAuth in Bitbucket?

Under Recent workspaces, select the workspace that will be accessed using the consumer; or find and open the workspace under All workspaces. On the sidebar, select Settings to open the Workspace settings. On the sidebar, under Apps and features, select OAuth consumers. Click the Add consumer button.


1 Answers

  1. First you create an "Oauth Consumer" in APPS AND FEATURES section of your bitbucket account setting. This gives you a "Key" and a "Secret".

  2. Now using these Key and Secret you ask Bitbucket for a token. In my case I made a http request to https://bitbucket.org/site/oauth2/access_token. In your case you should use a .net equivalent. I could do it with Curl or some Ajax library like this:

    curl -X POST -u "yourKeyHere:yourSecretHere"  https://bitbucket.org/site/oauth2/access_token -d  grant_type=client_credentials
    

alternatively, my http request was like this (using superagent in node) with my Content-Type set to application/x-www-form-urlencoded:

    request.post("https://yourKeyHere:[email protected]/site/oauth2/      access_token").send('grant_type=client_credentials');`

the result is like this:

    {
       "access_token": "blah blah blah HXAhrfr8YeIqGTpkyFio=",
       "scopes": "pipeline snippet issue pullrequest project team account",
       "expires_in": 3600,
       "refresh_token": "hsadgsadvkQ",
       "token_type": "bearer"
    }
  1. Now that you have the token, send it in a request header: Authorization: Bearer {access_token}

More info here bitbucket's api doc

like image 194
Vahid PG Avatar answered Oct 19 '22 17:10

Vahid PG