22

I've been trying to plug into the Toggl API for a project, and their examples are all using CURL. I'm trying to use the C# wrapper which causes a bad request when trying to create a report, so I thought I'd use Postman to try a simple HTTP request.

I can't seem to get the HTTP request to accept my API Token though. Here's the example that they give (CURL):

curl -u my-secret-toggl-api-token:api_token -X GET "https://www.toggl.com/reports/api/v2/project/?page=1&user_agent=devteam@example.com&workspace_id=1&project_id=2"

I've tried the following HTTP request with Postman with a header called api_token with my token as the value:

https://www.toggl.com/reports/api/v2/project/?user_agent=MYEMAIL@EMAIL.COM&project_id=9001&workspace_id=9001

(changed ids and email of course).

Any help on how to use the CURL -u in HTTP would be appreciated, thanks.

Matt Brewerton
  • 866
  • 1
  • 6
  • 23
  • 5
    Well, in Postman, select Basic Auth, fill Username with my-secret-toggl-api-token, and Password with api_token. Refresh headers and there you go – hlscalon Jan 06 '16 at 16:19
  • I did this and I believe it's worked! I now get a different error but I think that's down to my permissions in our Toggl Team, thanks. – Matt Brewerton Jan 08 '16 at 09:37

3 Answers3

33

The easy way is adding credential into url as user:pass@ format.

https://my-secret-toggl-api-token:api_token@www.toggl.com/reports/api/v2/project/?page=...
        <---------------------------------->

Alternately you can use credential with your http header like below:

Authorization: Basic XXXXXX

Here XXXXXX is base64(my-secret-toggl-api-token:api_token)

Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85
  • I used the Basic Authorization in Postman and it returns a different error for me, which I now believe is related to my access within Toggl. Thank you for the answer, it helped a lot! – Matt Brewerton Jan 08 '16 at 09:36
  • Probably worth noting here that the `:api_token` part of this is the exact string required, e.g. `https://aaa111...bbb222:api_token@www.toggl.com/...` – LomaxOnTheRun Nov 13 '19 at 02:35
0

As explained to me in another post, you can pass the api token to the user property if you are using HttpWebRequest:

request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes($"my-secret-toggl-api-token:api_token")));
Joshua Vandenbor
  • 509
  • 3
  • 6
  • 19
0

Using fetch

In node environment, your request might look something like so:

const url = 'https://www.toggl.com/reports/api/v2/project/page=1&user_agent=devteam@example.com&workspace_id=1&project_id=2';
const token = 'my-secret-toggl-api-token:api_token';

fetch(url, {
  method: 'GET', // not required
  headers: {
    Authorization: `Basic ${Buffer.from(String(token)).toString('base64')}`,
  },
})
Lior Elrom
  • 19,660
  • 16
  • 80
  • 92