简体   繁体   中英

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.

I am using an API that provides a token called "Personal Access Token". 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. I am making a POST request.

The api provides this info:

Link to send Post Request: 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"".

This is why I include the token in the in header ("I'm guessing it is a Bearer Token)

The response I get from the request is 200 but it doesn't perform the action I expect it to.

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.

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.

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.

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.

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.

Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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