I am trying to write a simple command line google drive api in Go. It seem to me so far I have succeeded in authenticating the application as I can get access_token and the refresh_token. The problem happens when I try to access the SDK Api using the token, I get the below error message
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
Another weird thing I noticed is that I do not see any quota information in my google api console. So not sure if that is the problem. But since I can be authenticated then I guess I should be fine in term of console api setup.
Below is the code for the api query
accessUrl := "https://www.googleapis.com/drive/v2/files" + "?access_token=\"" + accessToken + "\""
if res , err := http.Get(accessUrl); err == nil {
if b, err2 := ioutil.ReadAll(res.Body); err2 == nil {
fmt.Println(string(b))
}else{
fmt.Println(err2)
}
}else{
fmt.Println(err)
}
This happened to me because either:
So first check to make sure your token's not expired, the default is 3600 seconds or one hour, next if you're unsure you can always refresh the token.
And remember the tricky thing is once an app has been authorized subsequent requests to the server won't return a refresh token, which I think is kind of silly, but regardless that's the way it is. So the first auth you can get a refresh token, versus subsequent requests you can't.
My code for getting a new access token using a refresh token looks like this:
public static String refreshtoken(String refreshToken, SystemUser pUser) throws IOException {
HttpParams httpParams = new BasicHttpParams();
ClientConnectionManager connectionManager = new GAEConnectionManager();
HttpClient client = new DefaultHttpClient(connectionManager, httpParams);
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("refresh_token", refreshToken));
pairs.add(new BasicNameValuePair("client_id", "YOUR_CLIENT_ID"));
pairs.add(new BasicNameValuePair("client_secret", "YOUR_CLIENT_SECRET"));
pairs.add(new BasicNameValuePair("grant_type", "refresh_token"));
post.setEntity(new UrlEncodedFormEntity(pairs));
org.apache.http.HttpResponse lAuthExchangeResp = client.execute(post);
String responseBody = EntityUtils.toString(lAuthExchangeResp.getEntity());
ObjectMapper mapper = new ObjectMapper(); // can reuse, share
// globally
Map<String, Object> userData = mapper.readValue(responseBody, Map.class);
String access_token = (String) userData.get("access_token");
String token_type = (String) userData.get("token_type");
String id_token = (String) userData.get("token_type");
String refresh_token = (String) userData.get("refresh_token");
return access_token;
}
I am using Google App Engine and as a result have to use GAEConnectionManager, you get those details here: http://peterkenji.blogspot.com/2009/08/using-apache-httpclient-4-with-google.html.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With