Getting null Refresh token

时光怂恿深爱的人放手 提交于 2019-11-29 16:05:07

When building the request Url, you should set the Access Type :

requestUrl = new GoogleAuthorizationCodeRequestUrl(googleClientId, callBackUrl, scopes).setAccessType("offline").build();

As described in this page setting this parameter is recommended :

[...] We recommend that you explicitly set the access_type parameter to offline because we anticipate that when the online value is introduced, it will be as the default behavior. This could cause unexpected changes in your application since it would affect the way that your application is allowed to refresh access tokens. By explicitly setting the parameter value to offline, you can avoid any changes in your application's functionality. [...]

In addition to PapelPincel's answer, I had to also force the approval prompt using the .Net release 1.8.1.970 to get the refresh token. eg

var authReq = new GoogleAuthorizationCodeRequestUrl(new Uri(GoogleAuthConsts.AuthorizationUrl)) { 
    RedirectUri = Callback, 
    ClientId = ClientId, 
    AccessType = "offline", 
    Scope = string.Join(" ", new[] { Scopes... }), 
    ApprovalPrompt = "force" 
}; 

For anyone getting here from a Google search, I was not using the pure server side flow, so was getting the authorization token via javascript as in this doc, @PapelPincel answer was a hint for me.

You should add data-accesstype="offline" to your button as in the following snippet:

Example:

          <span
            data-accesstype="offline"
            class="g-signin"
            data-callback="signinCallback"
            data-clientid="CLIENT_ID"
            data-redirecturi="postmessage"
            data-cookiepolicy="single_host_origin"
            data-requestvisibleactions="http://schemas.google.com/AddActivity"
            data-scope="https://www.googleapis.com/auth/plus.login">
          </span>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!