asifshaon / 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

Refresh token is missing in the Calendar and Task Apis #174

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello!

 I'm using the Calendar and Task APIs in my web application. Before this day (08/12/2011) everything was working fine. Now when I request access token I'm getting a response without refresh token.Access token is available in the response.So i am getting the error:
" DotNetOpenAuth.Messaging.ProtocolException: Precondition failed: 
!authorization.AccessTokenExpirationUtc.HasValue || 
authorization.AccessTokenExpirationUtc < DateTime.UtcNow || 
authorization.RefreshToken != null"
 I tried to run the sample code(Samples.zip) downloaded from the location

 http://code.google.com/p/google-api-dotnet-client/wiki/Downloads#Latest_Stable_Release

 Then also i am getting the same error. How can i correct this? Please help.
 Best regards 
-Sreedevi Ajith

Original issue reported on code.google.com by sreedevi...@gmail.com on 15 Dec 2011 at 9:18

GoogleCodeExporter commented 9 years ago
Hello Sreedevi,

Unfortunately I am unable to reproduce your issue, I do believe that it is real 
as there were changes recently that should have affected this. Getting it fixed 
is a top priority. Can you add a parameter to your OAuth request: 

access_type=offline

Original comment by asky...@google.com on 15 Dec 2011 at 9:15

GoogleCodeExporter commented 9 years ago

Original comment by asky...@google.com on 15 Dec 2011 at 9:15

GoogleCodeExporter commented 9 years ago
Sreedevi,

Can you answer the following questions:
1. What version of .NET/Mono are you running.
2. What version of dotnetopenauth do you have installed/running for this 
application? (If possible)
3. What version of the google APIs do you have (even if you downloaded from the 
latest stable, something might have gone wrong in your end or our end hosting. 
Can you please check the version?)

Thanks,
Austin Skyles, Google Inc.

Original comment by asky...@google.com on 16 Dec 2011 at 6:16

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago

Hi Austin,

The details of the version are
1. Microsoft Visual Studio 2010 Version 10.0.40219.1 and Microsoft .NET 
Framework
Version 4.0.30319 
2. I am using the DotNetOpenAuth.dll Version 4.0.0.11165.It was included from 
the  downloads 
google-api-dotnet-client-1.1.4344-beta.samples\Lib\ThirdParty\DotNetOpenAuth.dll
3. GoogleApis.dll Version 1.1.4344.23417.It was included from the downloads 
google-api-dotnet-client-1.1.4344-beta.samples\Lib\Google.Apis.dll

I got the google-api-dotnet-client-1.1.4344-beta.samples.zip from the location
http://code.google.com/p/google-api-dotnet-client/wiki/Downloads#Latest_Stable_R
elease --- Samples.zip

Running the GoogleApisSamples.sln in VS 2010 with Tasks.ASP.NET.SimpleOAuth2 
project as the start up project.

Could you please tell me how can we add access_type=offline in the url.It seems 
that only the scope is generating from the code,not the url.If i am adding this 
parameter,it is coming as a part of the scope.

Thanks,
Sreedevi

Original comment by sreedevi...@gmail.com on 19 Dec 2011 at 5:22

GoogleCodeExporter commented 9 years ago
hi.
I've found the same bug since 21.12.2011. I use 
Google.Apis.Authentication.OAuth2, Version=1.0.0.19547 (which refers on 
DotNetOpenAuth)
I call the following query:
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=955661016
833.apps.googleusercontent.com&redirect_uri=http://social-local.com/layouts/syst
em/Social/SocialLogin.ashx?type=google_access&scope=https://www.googleapis.com/a
uth/plus.me 
https://www.googleapis.com/auth/userinfo.email&access_type=offline&state=someDat
aHere
and successfully retrieve the code in a response. But afterwards, trying to get 
the access token I retrieved the same exception: “Precondition failed: 
!authorization.AccessTokenExpirationUtc.HasValue || 
authorization.AccessTokenExpirationUtc < DateTime.UtcNow || 
authorization.RefreshToken != null”

Thanks in advance.
Den

Original comment by Chernov....@gmail.com on 21 Dec 2011 at 3:04

GoogleCodeExporter commented 9 years ago
Den,

Thanks for your comments. Can you show me the code where you are setting 
access_type? Setting access_type=offline will force the refresh token to be 
supplied. Can you not set access_type when retrieving the access token?

Original comment by asky...@google.com on 21 Dec 2011 at 4:48

GoogleCodeExporter commented 9 years ago
hi,

I could get the refresh token by providing the parameters 
access_type=offline&approval_prompt=force.When i tried with access_type=offline 
alone,it didnt give the refresh token.

Thanks,

Sreedevi

Original comment by sreedevi...@gmail.com on 22 Dec 2011 at 3:39

GoogleCodeExporter commented 9 years ago
Here is my code:
Step 1 (code receiving):

      this.provider.ClientIdentifier = this.appId;
      this.provider.ClientSecret = this.appSecret;

      List<string> scopes = new List<string>();
      scopes.Add(Paths.Google.API.ScopePluseMe);
      scopes.Add(Paths.Google.API.ScopeEmail);
      var state = new AuthorizationState(scopes);
      state.Callback = new Uri(request.Url.Scheme + "://" + request.Url.Host + Paths.SocialLoginHandlerPath + "?" + "type=google_access");

      Uri ur = this.provider.RequestUserAuthorization(state);
      string requestUrl = ur.ToString();
      if (this.isGetOfflineAccess)
      {
        requestUrl += "&access_type=offline";
      }

      requestUrl += "&state=" + appId + "_" + appSecret;

      this.response.Redirect(requestUrl);

WHERE:
provider is 
Google.Apis.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient

Constants value:
Paths.Google.API.ScopePluseMe: https://www.googleapis.com/auth/plus.me
Paths.Google.API.ScopeEmail: https://www.googleapis.com/auth/userinfo.email

state.Callback value:
http://social-local.com/layouts/system/Social/SocialLogin.ashx?type=google_acces
s

value of requestUrl:
https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=955661016
833.apps.googleusercontent.com&redirect_uri=http://social-local.com/layouts/syst
em/Social/SocialLogin.ashx?type=google_access&scope=https://www.googleapis.com/a
uth/plus.me 
https://www.googleapis.com/auth/userinfo.email&access_type=offline&state=someSta
te

Step 2 (AccessToken receiving): 
        var code = this.request.QueryString.Get("code");

        if (!string.IsNullOrEmpty(code))
        {
          this.provider.ClientIdentifier = this.appId;
          this.provider.ClientSecret = this.appSecret;

          List<string> scopes = new List<string>();
          scopes.Add(Paths.Google.API.ScopePluseMe);
          scopes.Add(Paths.Google.API.ScopeEmail);
          var state = new AuthorizationState(scopes);
          state.Callback =
            new Uri(
              request.Url.Scheme + "://" + request.Url.Host + Paths.SocialLoginHandlerPath + "?"
              + Paths.Google.QueryCommands.Access);

          this.auth = this.provider.ProcessUserAuthorization(code, state);

          var oAuth2Auth = new OAuth2Authenticator<NativeApplicationClient>(this.provider, this.GetAuthentication);

          // get user profile
          var objService = new PlusService(oAuth2Auth);
          var me = objService.People.Get("me").Fetch();

Where: 
state.Callback value is:
http://social-local.com/layouts/system/Social/SocialLogin.ashx?type=google_acces
s

Original comment by Chernov....@gmail.com on 22 Dec 2011 at 9:16

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks, guys!
The trick with approval_prompt=force also helped me.
But I think it's a temporary decision and RefreshToken should be received after 
using of access_type=offline without approval_prompt parameter. May I ask you 
to inform me when the problem will be solved.

Thanks in advance.
Den.

Original comment by Chernov....@gmail.com on 22 Dec 2011 at 9:42

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Thanks,

Could you please tell me how can we add access_type=offline in the url.It seems 
that only the scope is generating from the code,not the url.If i am adding this 
parameter,it is coming as a part of the scope.

Here  is my Code:
  private IAuthorizationState GetAuthorization(WebServerClient client)
        {
            // If this user is already authenticated, then just return the auth state.     
            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) || !string.IsNullOrEmpty(state.RefreshToken)))
            {                  // Store and return the credentials.           
                HttpContext.Current.Session["AUTH_STATE"] = _state = state; return state;
            }                // Otherwise do a new authorization request.          
            string scope = TasksService.Scopes.TasksReadonly.GetStringValue();
            OutgoingWebResponse response = client.PrepareRequestUserAuthorization(new[] { scope });
            response.Send(); // Will throw a ThreadAbortException to prevent sending another response.    
            return null;
        }

Original comment by zhanghon...@163.com on 5 Jan 2012 at 9:11

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I get an HTTP 400 error response myself if I try to include access_type=offline 
in my access_token request (grant_type=authorization_code) -- everything works 
fine if I leave it out, though then I don't get the refresh_token of course). 
Also, adding approval_prompt=force has no effect in my case. Still an HTTP 400 
error.

I also get a 400 when making a request for a new access_token via an old 
refresh_token (ie, grant_type=refresh_token). My requests look just like the 
latest API docs [1], so it does seem like something is broken on Google's end.

[1] http://code.google.com/apis/accounts/docs/OAuth2WebServer.html#offline

Original comment by ehsanul...@gmail.com on 6 Jan 2012 at 12:32

GoogleCodeExporter commented 9 years ago
@zhanghon...@163.com:

response.Headers["Location"] += "&access_type=offline&approval_prompt=force";
response.Send();

Hope this helps.
Kha

Original comment by kha.th...@gmail.com on 6 Jan 2012 at 1:50

GoogleCodeExporter commented 9 years ago
@ehsanul...@gmail.com:
Locate the correct "Client ID for web applications"

Click on "Reset client secret..." and then update your new Client secret to the 
code.

Run your application again and it should work.

I just got this same issue and resolved.

Kha

Original comment by kha.th...@gmail.com on 6 Jan 2012 at 2:17

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
We can add access_type=offline&approval_prompt=force in the url through the 
following code.

string uri = 
GoogleAuthenticationServer.Description.AuthorizationEndpoint.ToString();
            uri = uri + "?access_type=offline&approval_prompt=force";
            Uri ur = new Uri(uri);
            GoogleAuthenticationServer.Description.AuthorizationEndpoint = ur;
            var provider = new WebServerClient(GoogleAuthenticationServer.Description);
            provider.ClientIdentifier = ConfigurationManager.AppSettings["GoogleClientIdentifier"].ToString();
            provider.ClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"].ToString();            
            var authenticator =
                new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization) { NoCaching = true };
            return authenticator;

Hope this helps.
Sreedevi

Original comment by sreedevi...@gmail.com on 10 Jan 2012 at 6:23

GoogleCodeExporter commented 9 years ago
Do you guys know if this issue still exists for other APIs like the Google 
Cloud Storage?

More specifically - if I supply a Google Cloud Storage related 'scope', a 
refresh token is not returned in response to my request.

Original comment by differen...@gmail.com on 2 Mar 2012 at 11:22

GoogleCodeExporter commented 9 years ago
This issue was fixed in CL 51be0b85b89c. It should be present in release 
1.1.4457-beta and newer.

Original comment by asky...@google.com on 25 Apr 2012 at 5:26

GoogleCodeExporter commented 9 years ago
Issue 185 has been merged into this issue.

Original comment by asky...@google.com on 25 Apr 2012 at 8:52