2015-04-22 61 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」:真正的 「方法」: 「提示」}} –

相關問題