2014-10-30 83 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