1

I would like to know if it is possible to call a Google API that requires auth such as the Google Calendar API using the Apache HttpClient, and no Google code or libraries. (and need the code to do it)

This is the code I have so far, its gets an auth error, What do you use for the user/password?

HttpPost request = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?key=mykey");
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY),
new UsernamePasswordCredentials(user, password));
HttpResponse response = client.execute(request);

Error:

"errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
1
  • note this is a non-user interactive process, it needs to check the calendar every hour and not have any expiring credentials Commented Oct 4, 2016 at 21:13

1 Answer 1

3
+50

You don't use Login and password you need to be authenticated once you are you will have an access token then you can call something like this.

https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?access_token={tokenFromAuth}

You can authenticate to Google with any language that is capable of a HTTP POST and a HTTP GET. Here is my walk though of the Auth flow to Google http://www.daimto.com/google-3-legged-oauth2-flow/ you will need to create a Oauth2 credentials on Google Developer console to start. Then its just a matter of asking the user for permission to access their data and requesting the access.

Sign up to request clarification or add additional context in comments.

15 Comments

there is no user, it is a bot? the user needs to enter their credentials in the bot's config, not a user interactive process
Client login is no longer supported by Google. Your user will need to authenticate at least once then your bot can run automatically. Some apis support services accounts however the code for that includes creating jwt tokens its much harder to get to work and the user will have to create their own project on Google Developer consol and pre approve any data they want to access
the user authentication states the token only last 1 hour?
Okay, I had the clientId wrong (had .apps.googleusercontent.com twice)
Just cause you really helped me out
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.