简体   繁体   English

api访问令牌发布请求,从何处开始

[英]api access token post request, where to start

I've been struggling learning Auth for several months now, it comes down to I don't know where to start, it seems there are a bunch of different methods. 我几个月来一直在努力学习Auth,但归结为我不知道从哪里开始,似乎有很多不同的方法。

I am using an API that provides a token called "Personal Access Token". 我使用的API提供的令牌称为“个人访问令牌”。 Does this mean it's a Bearer or Web Token? 这是否意味着它是承载或网络令牌? I'm lost with this terminology. 我迷上了这个术语。

They allow you to play with their API in their online tools. 他们允许您在其在线工具中使用其API。 I am making a POST request. 我正在发出POST请求。

The api provides this info: api提供此信息:

Link to send Post Request: www.hackerrank.com/restoflink 发送帖子请求的链接:www.hackerrank.com/restoflink

Request Headers: 请求标头:

{
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Content-Length": 190
}

Request Body: 请求正文:

{
    "username": "testing",
    "subject": "test",
    "message": "test",
    "send_email": "true",
    "force": "false",
    "hide_login_credentials": "true",
    "access_token": "Access Token Number"
}

Here is my code: 这是我的代码:

function onFormSubmission(e){
  var accessToken ="ACCESS_TOKEN";

  var options = {

    method: "post",  
    headers: {
      "Accept": "application/json",
      "Authorization": "Bearer " + accessToken
    },

    payload: {
      "username": "testing@gmail.com",
      "subject": "test",
      "message": "test",
      "send_email": "true",
      "force": "false",
      "hide_login_credentials": "true",
      "access_token": "ACCESS TOKEN",
      "muteHttpExceptions": "false",
      "contentType": "application/json"
    }


  }

  var response = UrlFetchApp.fetch("linkhere", options);


  Logger.log(response.getResponseCode())
  Logger.log(response.getContentText());  

}

When I run this code without the bearer token in the header, I get a "404 truncated server error, "Invalid Access Token"". 运行标头中没有承载令牌的代码时,出现“ 404截断的服务器错误,“无效的访问令牌””。

This is why I include the token in the in header ("I'm guessing it is a Bearer Token) 这就是为什么我在in头中包含令牌的原因(“我猜它是不记名令牌)

The response I get from the request is 200 but it doesn't perform the action I expect it to. 我从请求中得到的响应是200,但没有执行我期望的操作。

I'm confused on what adjustment I have to make, even though I'm getting at 200 response code, something isn't working with my request from Apps Script. 我对必须进行的调整感到困惑,即使我得到200个响应代码,也无法满足我来自Apps Script的请求。

I tried making the request from POSTMAN and the api's test tools and all my attempts worked, which makes me believe I am doing something wrong in my script. 我尝试从POSTMAN和api的测试工具发出请求,所有尝试都成功了,这使我相信自己的脚本做错了什么。

Any help would be greatly appreciated, this post already helped out a lot ! 任何帮助将不胜感激, 这篇文章已经帮了很多忙

Here was my error: 这是我的错误:

UrlFetchApp.fetch("www.hackerrank.com/x/api/v2/tests?duration=-1&access_token=123", options)

URL: "" 网址:“”

I had to add 'https://', for a while I used 'http' and that didn't work. 我不得不添加“ https://”,有一段时间我使用了“ http”,但是没有用。

They call it a permanent OAuth token in their documentation (linked from your comments), but the way they use it is very simple and not like the OAuth implementations I've worked with in the past. 他们在文档中称它为永久性OAuth令牌(通过您的评论链接),但是使用它的方式非常简单,与我过去使用过的OAuth实现不同。

You don't need to include the access token in your headers, simply append &access_token=[your token] to the URL ( "linkhere" ) of your request. 您不需要在标头中包含访问令牌,只需将&access_token = [您的令牌]附加到请求的URL( "linkhere" )上。

Example: 例:

UrlFetchApp.fetch("www.hackerrank.com/x/api/v2/tests?duration=-1&access_token=123", options)

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

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