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!
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With