7eggs / node-toggl-api

Toggl API for Node.js
http://7eggs.github.io/node-toggl-api/
MIT License
121 stars 44 forks source link

Get my user ID? #8

Open darkpixel opened 8 years ago

darkpixel commented 8 years ago

If I use curl to authenticate using an API token to the Toggl API, it returns a blob of JSON that includes my user ID.

If I authenticate using node-toggl-api and a username and password and call the authenticate() method, I get a blob of data back with my user ID.

But if I call authenticate() when using an API Token, I get an error Error: No need to authenticate thus you use apiToken and I don't see a way to get my user ID. Am I missing something?

darkpixel commented 8 years ago

Temp work-around:

var toggl = new api({apiToken: process.env.TOGGL_API_KEY});

var req = {
  method: 'GET',
  auth: {}
};

req.auth[process.env.TOGGL_API_KEY] = 'api_token';

toggl.apiRequest('/api/v8/me', req, function(err, authdata) {
  if (err) {
    debug(err);
  } else {
    // do whatever
  }
});
estliberitas commented 7 years ago

@darkpixel I'm kind of late here, sorry. Is it still actual?

darkpixel commented 7 years ago

It may be, but I switched jobs and no longer have to provide reports based on Toggl data. ;)

4lch4 commented 6 years ago

I can confirm that using the authenticate(callback) method does still return the same error that @darkpixel mentioned: Error: No need to authenticate thus you use apiToken.

However, using the getUserData(options, callback) method will give you your user id in the JSON response object under the id property like so:

const config = require('./config')
const TogglClient = require('toggl-api')
const toggl = new TogglClient({apiToken: config.togglToken})

toggl.getUserData({}, (err, userData) => {
  if (err) console.log(err)

  console.log(userData.id)
})

EDIT: It appears PR #11 should fix the authenticate(callback) method I referred to.