1

I'm trying to get the planning from Teamweek in my js app. (API)

function get_teamweek_planning()
{
    var api_url     = 'https://teamweek.com/api/v2/',
        api_key     = '1234567',
        object_type = 'projects';

    $.ajax(
    {
        url: api_url + api_key + '/projects.json',
        type: 'GET',
        dataType: 'jsonp',
        cache: false,
        data: {},

        success:function(response){
            console.log('response:', response);
        }
    });     
}

This returns a 403 (forbidden). This is documented and means authentication fails. But how do i fix it?

Mettin Parzinski
  • 838
  • 1
  • 7
  • 13
  • Not sure from their documentation example using curl, but try adding `auth_token` in data: `data: {auth_token: your_token_here}` and if that fails perhaps try the same in the ajax headers key, something like `headers: {"Authorization": "auth_token " + your_token_here}` – Fiver Oct 24 '13 at 12:44
  • Those don't work... tried them all: function get_teamweek_planning() { var api_url = 'https://teamweek.com/api/v2/', api_key= '1234567'; $.ajax({ url:api_url + api_key + '/projects.json', type:'GET', dataType:'jsonp', cache:false, /*data:{auth_token: api_key}, */ /*headers:{"Authorization": "auth_token " + api_key}, */ /*beforeSend:function(req) { req.setRequestHeader('auth_token', api_key);}, */ success:function(response){ console.log(response);}, error:function (jqXHR, textStatus, errorThrown){ console.error('error getting teamweek planning:', jqXHR);}}); } – Mettin Parzinski Oct 25 '13 at 14:34
  • Nobody can read that, here's a pen: http://codepen.io/mettin/pen/Garko – Mettin Parzinski Oct 25 '13 at 14:45
  • Are you sure you are using the token and not the user id? From their docs: `The token can be found on a users profile page.` – Fiver Oct 25 '13 at 15:16
  • Yes. A nice weird string... – Mettin Parzinski Oct 25 '13 at 15:30
  • Just tried it myself with a free account. Their API is borken. It actually lets you log in via the token as a URL parameter, and I can get it to distinguish between a good and bad token. I'd email their support. – Fiver Oct 25 '13 at 16:04
  • Okay thanks @Pathétique! I've tried twitter support but they're not real helpfull so far (in all fairness, it's only been 2 days): https://twitter.com/TeamWeekPlan/status/393351602704506880 – Mettin Parzinski Oct 25 '13 at 19:15

2 Answers2

3
function get_teamweek_planning()
{
    var api_url     = 'https://teamweek.com/api/v2/',
        account_id  = '<your account id>';

    $.ajax(
    {
        url: api_url + account_id + '/projects.json',
        beforeSend: function (request)
        {
         request.setRequestHeader('Authorization', 'Bearer ' + btoa('<your api token>'))
        },
        type: 'GET',
        dataType: 'jsonp',
        cache: false,
        data: {},

        success:function(response){
            console.log('response:', response);
        }
    });     
}

If your're using this on a page that isn't only for yourself then do not hard code your token...

refiito
  • 56
  • 1
  • Thanks this worked! The problem is that the API is unclear what i should enter as account_id and api token. I provided the account_id and public_view_token which can be found as a data attribute on the body when logged in to Teamweek. – Mettin Parzinski Oct 31 '13 at 15:33
  • Any idea on how to get tasks for 1 user? – Mettin Parzinski Nov 01 '13 at 09:21
  • No such limitations as of this time... Current way is something like getting the tasks and then filtering out needed ones. You could use underscore's _.filter() for that. Also, you might try opening an issue on the teamweek api docs repo at github, it might get implemented :) – refiito Nov 06 '13 at 10:35
  • Filtering the array in JS isn't the problem. Efficiency is =) Any idea on how to get a error/callback from a jsonp call when the user presses 'cancel' when prompted for their teamweek user/pass? I'll do just that! – Mettin Parzinski Nov 06 '13 at 14:05
0

You forgot the authentication part, you need to authenticate on every request (as is documented on github).

Include the following vars for in the data object:

data: {
    auth_token: [your token],
    name: [client name]
}

https://github.com/toggl/teamweek_api_docs/blob/master/chapters/authentication.md

Frank de Jonge
  • 1,607
  • 1
  • 15
  • 23