Michael Tallino
Michael Tallino

Reputation: 821

OAuth2 - Got token, how to send with POST request?

Working with a vendor with minimal API and new to OAuth2. Using npm packages oauth and request.

Creating a node module which would post data from a database to the server's API.

I've got the authorization token, but now I'm unsure how to send that with the data post request. The vendor server is responding with a simple Cannot POST /accounts/import.

So - how do I send the OAuth2 token back with the post data to the server?

var request = require('request'),
  clcreds = require('../../creds/creds.js');


var OAuth = require('oauth');
var OAuth2 = OAuth.OAuth2;
var oauth2 = new OAuth2(clcreds.clientId, clcreds.clientSecret, 'https://URL.com/', null, 'token', null);
oauth2.getOAuthAccessToken('', {
  'grant_type': 'client_credentials'
}, function(e, access_token, refresh_token, results) {
  console.log(e, access_token, refresh_token, results);

  request.post('https://URL.com/accounts/import', {
    "accounts": [{
      ...
    }]
  }, function(e, r, body) {
    //returns Cannot POST /accounts/import
  })
})

Upvotes: 1

Views: 1742

Answers (1)

Takahiko Kawasaki
Takahiko Kawasaki

Reputation: 18991

Most resource servers support RFC 6750, 2.1. Authorization Request Header Field. If your server supports it, you can pass an access token to the server by adding Authorization header like below.

Authorization: Bearer YOUR-ACCESS-TOKEN

If your server does not support any means defined in RFC 6750, you have to ask the implementor of the server about how to pass an access token to the server.

Upvotes: 1

Related Questions