How to make Bitbucket API calls with access token?
Asked Answered
A

1

6

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!

Adrenal answered 28/7, 2014 at 15:54 Comment(0)
I
7
  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

Irrawaddy answered 22/3, 2017 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.