Use Oauth 2.0 in Google engine using java

I would like to use Oauth 2 for an application in Google App Engine with Java, but I did not find a good example of this use, I would be very grateful if anyone could help me, this is something unpleasant, examples, thnak you.

+4
source share
3 answers

My 2c avoids oauth2 libraries. Of course, opinions may be different, but for me they provide very impenetrable abstractions, so you get into the understanding of oauth behind the back door. At least for me, after spending an hour to read two pages that will tell you everything you need to know, and carefully avoiding everyone else, they will take you to where you want to be.

Simply put, the steps are: -

  • Call the authorization URL with your application / client ID and required areas. Turn on the email area.

  • Google will lead the user through the login and (if for the first time) authorization dialogs

  • In the end, the browser will redirect back to your oauthcallback URL and give you auth code
  • Call Google to convert the authentication code to an update token. This will also return the Google user ID and access token.
  • Save the user ID in the session to identify the user later.
  • Store update token along with google user id in database

On subsequent visits ...

  • If you have a google user id in your session, you can get the update token from your database and use it to generate access tokens as needed.
  • If you don’t have a google username in your session, follow the steps above. This time, Google will not ask the user for authorization (since it is already allowed), and the update token will be empty (since you already have one file).

All you need to know is on the oauth playground page. If you click on the buttons, you will see that they follow the steps described above.

Then you need to deal with possible error situations like

  • user denies permission
  • user removes permission.
  • google expired update token (it happens a lot), so you need to restart
  • timeouts

Two pages you should read: - https://developers.google.com/accounts/docs/OAuth2WebServer and the oauth playground at https://developers.google.com/oauthplayground/

Believe me, if you know how to generate the URL, save the update token (this is just a string) and parse the JSON response, then all you need is on these pages. Besides...

all documentation skips the need to store the user ID in your session so that you know who it is when you access your application. If you work in AppEngine, you may be confused by the code of the sample application, which uses a separate entrance to register applications. Ignore it. You will use oauth to authenticate the user so that the appengine application is not used and is somewhat confusing.

In fact, it is much simpler than some of the documentation would make you believe, and, as I said, imho leaky libraries do not help.

+10
source

I try to do the same, and I agree - it is very difficult to find a good example of this.

I really found this youtube video, and I think it would help: https://www.youtube.com/watch?v=tVIIgcIqoPw .

Its from Google, and it's called "Getting Started with the Google API." The last segment of the video is about authentication.

+1
source

There are several OAuth 2 client and server libraries for Java listed on this page: http://oauth.net/2/

Here's a quick tutorial on using Apache Otlu: https://cwiki.apache.org/confluence/display/OLTU/OAuth+2.0+Client+Quickstart

If you access the Google API (as a client), you can use the Google client library for Java, which runs OAuth, as well as setting up the API: https://code.google.com/p/google-api-java-client/

0
source

Source: https://habr.com/ru/post/1500170/


All Articles