Gáb*_*kos 6 c# oauth bitbucket restsharp bitbucket-api
我创建了一个 ASP.NET MVC 应用程序,它可以在 Bitbucket 上授权用户。我使用CSharp.Bitbucket 库来获取令牌秘密和令牌值。
该OAuth的教程说,与令牌,我可以进行API调用。
我知道我可以像这样使用基本授权调用 API:
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();
}
Run Code Online (Sandbox Code Playgroud)
但是如何使用访问令牌调用 API?
非常感谢!
首先,您在 bitbucket 帐户设置的应用程序和功能部分创建一个“Oauth 消费者”。这给你一个“钥匙”和一个“秘密”。
现在使用这些 Key 和 Secret 向 Bitbucket 索取令牌。就我而言,我向https://bitbucket.org/site/oauth2/access_token
. 在您的情况下,您应该使用 .net 等效项。我可以用 Curl 或像这样的一些 Ajax 库来做到这一点:
curl -X POST -u "yourKeyHere:yourSecretHere" https://bitbucket.org/site/oauth2/access_token -d grant_type=client_credentials
Run Code Online (Sandbox Code Playgroud)
或者,我的 http 请求是这样的(在节点中使用超级代理),我的Content-Type
设置为application/x-www-form-urlencoded
:
request.post("https://yourKeyHere:yourSecretHere@bitbucket.org/site/oauth2/ access_token").send('grant_type=client_credentials');`
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
{
"access_token": "blah blah blah HXAhrfr8YeIqGTpkyFio=",
"scopes": "pipeline snippet issue pullrequest project team account",
"expires_in": 3600,
"refresh_token": "hsadgsadvkQ",
"token_type": "bearer"
}
Run Code Online (Sandbox Code Playgroud)
Authorization: Bearer {access_token}
这里有更多信息bitbucket's api doc
归档时间: |
|
查看次数: |
3123 次 |
最近记录: |