简体   繁体   中英

Reusing Google Api Bearer Token to access user's Drive

I have the following:

   gapi.auth.authorize(
        { client_id: CLIENT_ID, scope: SCOPES, immediate: false },
        handleAuthResult);

Which gives me access to an access_token :

在此处输入图片说明

The goal is that a user of this application can grant access to my app to use their google drive to store their content. I need to be able to use this token to:

  1. Read files
  2. Post files

Over a long period of time without reauthenticating via the google login page. How can this be achieved? I'm using C# as a backend.

You will need to switch from doing OAuth in JavaScript in the browser to doing OAuth on your server. It's well documented here https://developers.google.com/identity/protocols/OAuth2WebServer You have the choice of using the OAuth c# library, or simply calling the REST endpoints.

To achieve access without the user present, you'll need access_type=offline which ...

Set the value to offline if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token and an access token the first time that your application exchanges an authorization code for tokens.

You will then get a Refresh Token on the first time the user grants permission. You will need to save this Refresh Token in some sort of server datastore. You can then use it at any time to request an Access Token.

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