jalendport / craft-fetch

Utilise the Guzzle HTTP client from within your Craft templates.
MIT License
24 stars 13 forks source link

Authentication with bearer token #3

Closed rachelrine closed 5 years ago

rachelrine commented 5 years ago

Hi Luke,

I'm successfully using the plugin to retrieve a bearer token with basic auth, but I'm unsure as to how I should use this token in my next call.

After getting the token, here's what I've tried:

{% set client = {
    base_uri : 'https://my.url.com',
    timeout : 30
} %}
{% set options = {
    headers : ['Authorization', 'Bearer: ' ~ myToken]
} %}
{% set results = fetch(client, 'POST', 'path/to/endpoint', options) %}

And this is the error: Invalid or missing authorization head (truncated...)

If it's helpful, here's the instructions from the API on how to make that call. Appreciate any help you may have! Thanks.

rachelrine commented 5 years ago

Figured it out with the following:

{% set client = {
    base_uri : 'https://my.url.com',
    timeout : 30
} %}
{% set options = {
    headers : {
      Authorization: 'Bearer ' ~ myToken
    }
} %}
{% set results = fetch(client, 'GET', 'path/to/endpoint', options) %}