Message Boards Message Boards

GROUPS:

[✓] Include "Authorization: Bearer ..." in a URL execute?

Posted 1 month ago
254 Views
|
4 Replies
|
3 Total Likes
|

I am trying to access the Asana api. In order to do that I need to send http requests of the form

https://api end point "Authorization: Bearer 0/123456789abcdef"

I have tried setting Authetication -> <|"Authorization" -> Bearer... |> but that just brings up the asana sign in box. I also tried "Headers" -> "Authorization: Bearer..." |> but that throws the error

HTTPRequest::nvldheaders: -- Message text not found -- (Join[{user-agent->Wolfram HTTPClient 12.},Authorization: Bearer ...

What is the correct way to include the Authorization header request in the message?

4 Replies
Posted 1 month ago

Andrew,

HTTP headers have to be passed as a list. Try this

HTTPRequest[API_ENDPOINT_URL, <|"Headers" = {"Authorization: Bearer ACCESS_TOKEN"}|>]
Posted 1 month ago

Thanks for your suggestion, sadly I am still doing something wrong.

This curl command works correctly curl -H "Authorization: Bearer 0/123456789abcdef" https://app.asana.com/api/1.0/users/me

I am trying to reproduce that in WL. My current attempt is: header = <|"Headers" -> {"Authorization: Bearer 0/123456789abcdef"}|> URLExecute["https://app.asana.com/api/1.0/users/me", header]

Can you see anything wrong with my code, and also is there a way to get Mathematica to show me exactly what it is sending to Asana?

Andrew,

The format for a curl command of

curl -H "Authorization: Bearer 0/123456789abcdef"

should be

HTTPRequest[
  "url goes here", <|
   Method -> "POST",
   "Headers" -> {"Authorization" -> 
      "Bearer 0/123456789abcdef"},
   "Body" -> {"username" -> "myusername", "password" -> "mypwd"}
   |>
  ];

This assumes that there is a need for a username and pwd, You may not need a body if you don't need the login.

I hope this helps.

Regards,

Neil

Posted 1 month ago

That's fantastic. Problem solved, thank you very much.

For anyone who finds this thread whilst trying to contact Asana, the following works:

req = HTTPRequest[ "https://app.asana.com/api/1.0/users/me", <|Method -> "Get", "Headers" -> {"Authorization" -> "Bearer 0/abc123"}|>]

URLRead{req]

Please note, Asana requires a Get rather than a Post, hence the change to Neil's code. Also, using the Personal Access Token (PAT) means that you don't need to send the username and password

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract