liuqiaosz / google-api-dotnet-client

Automatically exported from code.google.com/p/google-api-dotnet-client
Apache License 2.0
0 stars 0 forks source link

How to get lost Refresh Token by Dot Net Client Library #287

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi All,

My Problem is this I could not save Refresh_Token with initial request to API. 
I know that I can get again my refresh token by setting Uri parameters 
"approval_prompt=force&access_type=offline". But why using the below code of 
client library how to set these parameters with Uri and where to set? so that I 
can get refresh_token again and save that in my database.

To get authentication & authorization for my Google Drive access my code is 
like this:

private static IAuthenticator CreateAuthenticator()
        {
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = ClientCredentials.CLIENT_ID;
            provider.ClientSecret = ClientCredentials.CLIENT_SECRET;
            return new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
        }
 and my authorization method is like this....

private static IAuthorizationState GetAuthorization(NativeApplicationClient 
client)
        {
            // You should use a more secure way of storing the key here as
            // .NET applications can be disassembled using a reflection tool.
            string scope = DriveService.Scopes.Drive.GetStringValue();

            IAuthorizationState state = AuthState;
             if (state != null)
             {
                 return state;
             }

             // Check if an authorization request already is in progress.
             state = client.ProcessUserAuthorization(new  
             HttpRequestInfo(HttpContext.Current.Request));
             if (state != null && (!string.IsNullOrEmpty(state.AccessToken)) )
             {
                 // Store and return the credentials.
                 HttpContext.Current.Session["AUTH_STATE"] = _state = state;
                 return state;
             }
            // Otherwise do a new authorization request.
            string scope = DriveService.Scopes.Drive.GetStringValue();
            OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });
            response.Send(); // Will throw a ThreadAbortException to prevent sending another response.
            return null;
        }
and in my Page_Load method of my ASP.Net web page code is like this :

protected void Page_Load(object sender, EventArgs e)
         {
             // Create the Tasks-Service if it is null.
             if (_service == null)
             {
                 _service = new DriveService(_authenticator =  
CreateAuthenticator());
             }

             // Check if we received OAuth2 credentials with this request;  
if yes: parse it.
             if (HttpContext.Current.Request["code"] != null)
             {
                 _authenticator.LoadAccessToken();
             }

             // Change the button depending on our auth-state.
             listButton.Text = AuthState == null ? "Authenticate" : "Fetch  
Drive Files";
         }

Please ignore if anything missing in above given code. Consider the above code 
is working. As I have accurate code in my office computer. Right now I have 
posted what I have and how I am doing authentication & authorization procedure.

Thanks in advance

Original issue reported on code.google.com by fasihakb...@gmail.com on 7 Feb 2013 at 4:25

GoogleCodeExporter commented 9 years ago

Original comment by pele...@google.com on 13 Apr 2013 at 4:31

GoogleCodeExporter commented 9 years ago
With the new Google.Apis.Auth library it's clear. you just set AccessType
see:
https://code.google.com/p/google-api-dotnet-client/source/browse/Src/GoogleApis.
Auth/OAuth2/Requests/GoogleAuthorizationCodeRequestUrl.cs#33

Original comment by pele...@google.com on 11 Dec 2013 at 1:07