2015-04-22 69 views
0

我试图获得刷新令牌,一旦用户授权与谷歌。 因此,用户不必再次授权。 我研究了Google的文档,并且我知道我必须将访问类型设置为离线。谷歌API获取refrest token:未捕获ReferenceError:脱机未定义

var cid = 'XXXXX'; 
var apik = 'XXXX'; 
var scopes = 'https://www.google.com/m8/feeds'; 


function authorizeWithGoogle() { 
    gapi.client.setApiKey(apik); 
    gapi.auth.authorize({ client_id: cid, scope: scopes, immediate: false,  accesstype: offline }, handleAuthResult);} 

function handleAuthResult(authResult) { 
    if (authResult && !authResult.error) { 
     console.log(JSON.stringify(authResult)); 
     $.get("https://www.google.com/m8/feeds/contacts/default/full?alt=json&access_token=" + authResult.access_token + "&max-results=11700&v=3.0", 
     handleGoogleContacts); 
    } 
} 

HTML代码:

现在,我有以下的javascript代码试图

<input type="submit" class="btn btn-info" value="Google" onclick="authorizeWithGoogle()"/> 

<script src="https://apis.google.com/js/client.js"></script> 

它给我以下错误:

Uncaught ReferenceError: offline is not defined

任何人可以帮助我吗?谢谢

回答

2

accesstype: offline < - 这是寻找一个变量离线,而不是一个字符串。用引号把它包起来。

accesstype: "offline"

下一个问题是,你正在使用一个提交按钮的事实,但你不取消表单提交所以页面会提交。

onclick="authorizeWithGoogle(); return false" 

有更好的方法来取消它,但会很好地工作内嵌的事件。

+0

谢谢你的回复。 现在,我得到了以下格式的结果,我没有得到刷新标记{“state”:“”,“access_token”:“”,“token_type”:“承载者”,“expires_in”:“3600”,“范围“:”https://www.google.com/m8/feeds https://www.googleapis.com/auth/contacts","client_id":"","re​​sponse_type":"token","issued_at“: “1429740898”, “expires_at”: “1429744498”, “状态”:{ “google_logged_in”:假的, “signed_in”:真正的 “方法”: “提示”}} –

相关问题