r/javahelp • • Dec 24 '20

Can someone help me with Oauth2 and Google api?

Hey guys I want to get my data from Google fit, so I guess I have to use the Google fit api but I don't really understand how Oauth2 works. Am I really obligated to use it if I just want to get my personal data? And if yes how can I make a http request with it? Thank in advance for helping me!

23 Upvotes

4 comments sorted by

•

u/AutoModerator Dec 24 '20

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions

  • You include any and all error messages in full

  • You ask clear questions

  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

10

u/E3FxGaming Dec 24 '20

Am I really obligated to use it if I just want to get my personal data?

Google builds their products for scale and thus offers solutions that scale well from 1 to many service consumers.

While you may think "well this is me. I want to access my own data, this should not be this complicated." consider that the same API access also has to work for someone that writes a fitness app for millions of future users.

And if yes how can I make a http request with it?

Google decribes it pretty well in their oauth docs.

To begin, obtain OAuth 2.0 client credentials from the Google API Console. Then your client application requests an access token from the Google Authorization Server, extracts a token from the response, and sends the token to the Google API that you want to access.

  1. You basically tell Google "I have an application that wants to access your APIs."

  2. Google asks "Cool, what do you want to call your application and which APIs does your application want to make use of?"

  3. You provide the information asked for

  4. Google gives you credentials that your application will use to identify itself as a client of the application you registered

So far the process was a single one for all future client applications. What happens next is something each client instance has to do:

  1. Your application performs a http request with the credentials to the central Google authentication servers. You include a scope parameter in the request to define to wich resources you want access.

2.You then grant an authorization for this specific data scope.

3.If everything goes well, Google will send you back an auth token.

This auth token is as valuable as longin credentials (for the granted data scope - a token for a fitness app probably wont allow Google Drive access, ...).

Do not commit this token to public version control systems. It can be used by anyone to identify themselves as you and gain insight into your health data.

When you perform http requests to the Google API, add an Authorization header field to your request header with the auth token as the header field value. This will tell Google on whose behalf your client performs requests.

I said that this auth token is as valuable as login credentials - you can really think of it as sending a username + password with every request, but the username and password were compressed into one Authorization field and are together probably way longer than the usual length of your username + password.

1

u/Errwann Dec 29 '20

Thank you very much for your response!

1

u/ratskinmahoney Dec 24 '20 edited Dec 24 '20

This is a very good, thorough answer about how OAuth will work and why its apparent complexity is worthwhile. Practically, though, you can make use of libraries that will do a lot of the work for you.

I would recommend taking a look at Spring's OAuth2RestTemplate. You don't need to be using a full Spring stack to use it. It will give you a fairly simple REST client that you prime with a simple configuration and it will handle the exchanges and headers and so on for you.

Note that OAuth2 defines a number of different "flows" which support different authentication scenarios (e.g. a user logging into an application, or a machine authenticating with an API). You are probably interested in the Client Credentials flow, which is the simplest of these.

There's a fairly simple example here of how to configure the REST template: https://gist.github.com/ekswai/2e263e2c8ca8583c0fd93014d32476a0