【问题标题】:How to make Bitbucket API calls with access token?如何使用访问令牌进行 Bitbucket API 调用?
【发布时间】:2021-08-19 15:24:13
【问题描述】:

我创建了一个 ASP.NET MVC 应用程序,它可以在 Bitbucket 上授权用户。 我使用CSharp.Bitbucket library 来获取令牌秘密和令牌值。

OAuth tutorial 说我可以使用令牌进行 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();
 }

但是如何使用访问令牌调用 API?

非常感谢!

【问题讨论】:

    标签: c# oauth bitbucket restsharp bitbucket-api


    【解决方案1】:
    1. 首先,您在 bitbucket 帐户设置的应用和功能部分创建一个“Oauth 消费者”。这给了你一个“钥匙”和一个“秘密”。

    2. 现在使用这些 Key 和 Secret,您可以向 Bitbucket 索要一个令牌。就我而言,我向https://bitbucket.org/site/oauth2/access_token 发出了http 请求。在您的情况下,您应该使用 .net 等效项。我可以使用 Curl 或像这样的一些 Ajax 库来做到这一点:

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

    或者,我的 http 请求是这样的(在节点中使用超级代理),我的 Content-Type 设置为 application/x-www-form-urlencoded

        request.post("https://yourKeyHere:yourSecretHere@bitbucket.org/site/oauth2/      access_token").send('grant_type=client_credentials');`
    

    结果是这样的:

        {
           "access_token": "blah blah blah HXAhrfr8YeIqGTpkyFio=",
           "scopes": "pipeline snippet issue pullrequest project team account",
           "expires_in": 3600,
           "refresh_token": "hsadgsadvkQ",
           "token_type": "bearer"
        }
    
    1. 现在您有了令牌,将其发送到请求标头中: Authorization: Bearer {access_token}

    更多信息在这里bitbucket's api doc

    【讨论】:

      猜你喜欢
      • 2013-07-22
      • 2012-09-26
      • 2019-11-19
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多