I have been working on getting google analytics data using the Google Core Reporting API using OAuth2. After the initial oauth dance is done, I store the access token and refresh token in the database to be able to regenerate valid access token from the refresh token. This is termed as offline access. This is all well documented here.

However, on one of my servers, I was getting an authentication error while generating the refresh token. The weird thing was it was only happening on one of the servers. I have 3 servers running, all Ubuntu 10.08 servers. This is what the error looked like :-

{"error": {"code": 401, "message": "Invalid Credentials", "errors": 
[{"locationType": "header", "message": "Invalid Credentials", 
"location": "Authorization"}]}} 

I will cut the chase here and say that the solution was a very strange one. Apparently, the time was not in sync between the server I was running and google’s server. It did take quite a while to figure this one out, hence this blog post. In the end, all I had to do on my Ubuntu server was this:-

sudo apt-get install ntp

Just to be totally sure, I also rebooted the server, although I don’t think that iwas required.

But after the npt sync, my calls to the google API to generate access tokens from a refresh token were working again.

Hope this saves someone some frustration!

Tagged with:  

3 Responses to Google oauth2 refresh token and invalid credentials error

  1. Many thanks! Very strange issue indeed.

    The package name is ntp, not npt, though.

  2. smd says:

    I’m having the same problem, I’m trying to authenticate to SMTP so I can fetch emails from my gmail account and process them on my server, but whenever I enter a brand new Token I receive a 500 error saying that my credentials are invalid. The weird thing is that the code works like a charm locally. Any idea what the problem might be? I’m getting very frustrated 🙁

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 446 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: