调用gapi.auth.authorize()会返回空的access_token

时间:2014-01-12 23:09:35

标签: google-api google-drive-api google-api-client

在调用gapi.auth.authorize()并通过此命令获取服务器端的访问令牌之后:

gapi.auth.authorize({ response_type: 'code', client_id: "client_id", immediate : false, access_type : 'offline', scope: "scopes", redirect_uri : "redirect_uri"} , handleAuth1);

我再次调用它来接收客户端的令牌:

gapi.auth.authorize({client_id: "client_id", immediate : true, scope: "scopes"}, handleAuth2);

由于某种原因,handleAuth2获得没有access_token的结果。在这种情况下,我的应用程序要求用户允许第二次访问。在这种情况下,由于新政策,Google会显示“有离线访问权限”。

我可以立即获得离线和在线权限吗?

编辑:我通过使用gapi.auth.signIn()解决了这个问题,因为Google添加了这个新API。诀窍是先用这些参数调用api:

  <span id="signinButton">
  <span
    class="g-signin"
    data-callback="loginFinished"
    data-clientid="808248997275-ol6kol8h23j018iug3d5odi9vhrja9j5.apps.googleusercontent.com"
    data-accesstype="offline"
    data-cookiepolicy="http://brif.us"
    data-scope="profile https://mail.google.com https://www.google.com/m8/feeds https://www.googleapis.com/auth/drive">
  </span>

使用接受的代码在服务器端生成刷新令牌。 下一个调用应省略离线,因此代码应为:

  <span id="signinButton">
  <span
    class="g-signin"
    data-callback="loginFinished"
    data-clientid="808248997275-ol6kol8h23j018iug3d5odi9vhrja9j5.apps.googleusercontent.com"
    data-cookiepolicy="http://brif.us"
    data-scope="profile https://mail.google.com https://www.google.com/m8/feeds https://www.googleapis.com/auth/drive">
  </span>

希望这有助于某人...

0 个答案:

没有答案