Question

I'm trying to create a user account through the apigee JS API. This worked just fine when I was last doing this before the holidays in mid December. Now, however, I get a 401 Unauthorized error reading token_expired.

Is there a way to refresh the token? I don't know why it would have expired.

This is what I'm trying. First I instantiate the data client. No problems here:

var dataClient;
var client_creds = {
    orgName: '*******',
    appName: '*******'
}
dataClient = new Apigee.Client(client_creds);

Later, when trying to create a new user, I get the token_expired error:

dataClient.request(options, function (error, response) {
        if (error) {
            console.log(response);
            alert("Something went wrong when trying to create the user. " + response.error)
            // Error
        } else {
            // Success - the user has been created, now login.
            dataClient.login(user.email, user.password,
                function (err) {
                    if (err) {
                        //error - could not log user in
                        console.log("There was an error logging in " + user.name);
                    } else {
                        //success - user has been logged in
                    }
                }
            );
        }
    });

I've also tried dataClient.signup, but same error.

Was it helpful?

Solution

There are no refresh tokens within App Services; you'll need to follow the login flow in order to retrieve a new token. Note that you can specify the ttl parameter, like so, so you don't need to do this as frequently:

https://api.usergrid.com/{org}/{app}/token?ttl=604800

By default, this is set to 7 days, but you can change the default app max ttl to 0 (non-expiring) or something else like 31104000000 (365 days).

To do that, you make a PUT request:

https://api.usergrid.com/{org}/{app}/?client_id={app_client_id}&client_secret={app_client_secret}

With JSON payload:

{
    "accesstokenttl":0
}

Or for 1 year:

{
    "accesstokenttl":31104000000
}

OTHER TIPS

If that doesn't work for you, the authorization tokens for the JavaScript SDK are kept in your browser's local storage. In Chrome, use the Developer Tools. In the Resources tab on the left hand side expand the Local Storage entry. You should see something like "http://usergrid.dev" or something similar. Choose that and on the right hand side you should see an entry for accessToken. Delete that and it should solve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top