Reputation: 1111
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!
Upvotes: 6
Views: 5549
Reputation: 428
First you create an "Oauth Consumer" in APPS AND FEATURES section of your bitbucket account setting. This gives you a "Key" and a "Secret".
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"
}
Authorization: Bearer {access_token}
More info here bitbucket's api doc
Upvotes: 7