goshippo / shippo-node-client

Shipping API Node.js library (USPS, FedEx, UPS and more)
https://goshippo.com/docs
MIT License
136 stars 54 forks source link

Ability to create shipments on behalf of Oauth users #53

Closed danschumann closed 4 years ago

danschumann commented 4 years ago

According to https://goshippo.com/docs/oauth/ , you must use "Bearer" instead of "ShippoToken" in the Authorization header.

So, a quick hack:

(Mind the coffee script)

ShippoResource = require('shippo/lib/Resource')
getHeaderOriginal = ShippoResource::_get_headers
ShippoResource::_get_headers = (data) ->
  output = getHeaderOriginal.apply this, arguments
  console.log 'cooool, it saved', @_shippo.get('oauthToken')
  if @_shippo.get('oauthToken')
    output.Authorization = 'Bearer ' + @_shippo.get('oauthToken')
  return output

shippo = require('shippo')('any api key, no longer used')
shippo.set('oauthToken', user.shippo_token)

# make normal requests like
shippo.shipment.create {...}

Shall I make a PR to implement something like this?
Would it be better off to just allow

shippo = require('shippo')('oauth.abc1234')

And detect the string starting with oauth... and therefore simply use Bearer instead of ShippoToken?

danschumann commented 4 years ago

Oh yea, I switched so that I can just pass the oauth token to the initial shippo

ShippoResource = require('shippo/lib/Resource')
getHeaderOriginal = ShippoResource::_get_headers
ShippoResource::_get_headers = (data) ->
  output = getHeaderOriginal.apply this, arguments
  if @_shippo.get('token').match(/^oauth/)
    output.Authorization = 'Bearer ' + @_shippo.get('token')
  return output

shippo = require('shippo')('oauth.abcasdflsjafdklj...)
danschumann commented 4 years ago

https://github.com/goshippo/shippo-node-client/pull/54