2014-10-30 112 views
3

我試圖遵循Google's developer tutorial
樣品例如使用更新(38.0版)gapi.auth JavaScript命令不執行正確

但好像從來沒有達到過的的gapi.auth功能它們的回調函數

這裏是一個代碼示例,演示它:

<!doctype html> 
<html> 
    <head> 
    <title>Google API Client test</title> 
    </head> 
    <body> 
    here will be the use of Oauth 2.0 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script>  
    googleApiClientReady = function() {  
    window.setTimeout(checkAuth, 1); 
    } 
    function checkAuth() {  
     gapi.auth.authorize({ 
    client_id: 'XXX', 
    scope: 'https://www.googleapis.com/auth/youtube', 
    immediate: true 
    }, handleAuthResult); 
    } 
    function handleAuthResult(authResult) { 
    if (authResult && !authResult.error) { 
    alert('gapi.auth return successfully'); 
    } else { 
    alert('gapi.auth return with error'); 
    } 
    } 
    </script> 
    <script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script> 
    </body> 
</html> 

當我運行上面的HTML + JS文件 - 「HandleAuthResult」的2個可選的警報沒有在所有屏幕上顯示- 這意味着該功能不受「gapi.auth.authorize」

叫回來

有沒有人設法正確使用這個庫?

回答

0

首先,您需要檢查chrome是否阻止了彈出窗口。這是因爲彈出式開放代碼不在點擊處理程序中。

然後,您應該打開chrome的開發者控制檯並檢查「控制檯」選項卡是否有錯誤。

你也應該指定你的域名和端口您的網頁位於在谷歌開發者控制檯是這樣的:Google developer console - javascript origins

我可以用你的代碼登錄到谷歌和接收成功的回調 但只有移除後「立即:真「選項。 我不知道爲什麼,但我得到「immediate_failed」錯誤。

+0

http://stackoverflow.com/questions/21710842/google-api-in-javascript – GyRo 2014-11-09 04:43:49

+0

謝謝。這很有幫助。另外,它只在我從一個web服務器(url以http啓動)運行html而不是直接通過文件系統運行時才起作用。 – GyRo 2014-11-09 14:46:33