Note that there are some explanatory texts on larger screens.

plurals
  1. POAuthorizing with stored credentials. Google Drive API for .NET
    text
    copied!<p>I'm trying to create desktop application which will allow to list files and folders on google drive account. On this momment I'm able to do it but there is a one issue. I have to re-login each time I want to open google drive account from my application. Is it possible to use stored locally AccessToken/Refresh tokens in order to avoid re-authorization each time?</p> <p>Here method which is used to get authorization. </p> <pre><code>private IAuthorizationState GetAuthorization(NativeApplicationClient arg) { IAuthorizationState state = new AuthorizationState(new[] { "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile" }); // Get the auth URL: state.Callback = new Uri("urn:ietf:wg:oauth:2.0:oob"); UriBuilder builder = new UriBuilder(arg.RequestUserAuthorization(state)); NameValueCollection queryParameters = HttpUtility.ParseQueryString(builder.Query); queryParameters.Set("access_type", "offline"); queryParameters.Set("approval_prompt", "force"); queryParameters.Set("user_id", email); builder.Query = queryParameters.ToString(); //Dialog window wich returns authcode GoogleWebBrowserAuthenticator a = new GooogleWebBrowserAuthenticator(builder.Uri.ToString()); a.ShowDialog(); //Request authorization from the user (by opening a browser window): string authCode = a.authCode; // Retrieve the access token by using the authorization code: return arg.ProcessUserAuthorization(authCode, state); } </code></pre> <p><strong>SOLVED:</strong></p> <p>In order to invoke methods from Google Drive sdk first you need to instance of service:</p> <pre><code> var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, GoogleDriveHelper.CLIENT_ID, GoogleDriveHelper.CLIENT_SECRET); var auth = new OAuth2Authenticator&lt;NativeApplicationClient&gt;(provider, GetAuthorization); Service = new DriveService(auth); </code></pre> <p>Those CLIENT_ID and CLIENT_SECRET you will have after you sign up for application in Google API console.</p> <p>Then you need to define GetAuthorization routine, which might look as following:</p> <pre><code>private IAuthorizationState GetAuthorization(NativeApplicationClient arg) { IAuthorizationState state = new AuthorizationState(new[] { "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile" }); state.Callback = new Uri("urn:ietf:wg:oauth:2.0:oob"); state.RefreshToken = AccountInfo.RefreshToken; state.AccessToken = AccountInfo.AccessToken; arg.RefreshToken(state); return state; } </code></pre> <p>It will works if you already have Refresh and Access tokens (at least Refresh). So you need to authorize for some user account first.</p> <p>Then you can use that Service instance to invoke sdk methods. Hope it will help someone.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload