简体   繁体   中英

Google API with JS Frontend and Java Backend

I want to implement a HTML/JS frontend for Google Drive to upload files into my own database in my software project. My backend will be written in Java .

The problem is : What is a feasible design for the user login process via Google's OAuth2 ?

I wanted to let the user click in the frontend on a link "login to google drive" and the JS sends a request to my java backend, which generates the OAuth2 url via google java api lib. Now the URL is send back to the browser and a new window is opened which lets the user login to google and google returns the access code to the browser (or my java webservice?).

I'm not an experienced software developer and wanted to ask how you would design/handle this login process. After the login I will use the token to list all the files and show them in the browser, etc.

I am just learning my first steps with java servlets (and tomcat) to create this small webservice.

Here is an example of handling oauth2 authorization: https://developers.google.com/accounts/docs/OAuth2Login

In short you have to get an authorization token and sign each request to the google api with it. There are different ways to obtain the token. You can get the token on the client side and send it to server side to reuse in the further requests, or you may handle it on server side. It is up to you.

To get it you have to:

  • redirect the user to a google login prompt
  • wait for the user to be redirected to the callback which you define in the google api console when you create the application
  • the result is going to contain the token or an error message

Wherever you get the token, store it on the server side and reuse it until it expires.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM