简体   繁体   English

使用Node.js的Bitbucket OAuth2

[英]Bitbucket OAuth2 using Node.js

I want to have a capability in my application that allows users to Authorize Bitbucket. 我想在我的应用程序中具有允许用户授权Bitbucket的功能。 I have followed https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html . 我遵循了https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html

The following works and brings the Bitbucket authorization screen as expected: https://bitbucket.org/site/oauth2/authorize?client_id= {client_id}&response_type=code 以下步骤可以正常工作,并按预期显示Bitbucket授权屏幕: https ://bitbucket.org/site/oauth2/authorize ? client_id = {client_id}&response_type = code

However, this part emits an error about invalid Grant. 但是,此部分发出有关无效授予的错误。

$ curl -X POST -u "client_id:secret" \
  https://bitbucket.org/site/oauth2/access_token \
  -d grant_type=authorization_code -d code={code}

I am using request module in Node.js and using the code as follows: 我在Node.js中使用request模块,并使用如下代码:

request.post(
  'https://bitbucket.org/site/oauth2/access_token',
  {
     json: {
         client_id: config.get('app.bitbucket_client_id'),
         client_secret: config.get('app.bitbucket_client_secret'),
         code: req.query.code,
         grant_type: "authorization_code"
        }
      }, function (error, response, body) {    
         // Do something here
      }
  }

{"result":{"error_description":"Unsupported grant type: None","error":"invalid_grant"}} {“结果”:{“ error_description”:“不支持的授予类型:无”,“错误”:“ invalid_grant”}}

Please advice! 请指教!

Found the answer to my question. 找到了我问题的答案。 Basically, Bitbucket expects data to be sent in body directly instead of JSON. 基本上,Bitbucket希望数据直接在正文中发送,而不是JSON。 Changing from json to form in the code above fixed it for me. 从上面的代码中的json更改为form对我来说已经修复了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM