Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中的googleapi_Javascript_Google Api - Fatal编程技术网

Javascript中的googleapi

Javascript中的googleapi,javascript,google-api,Javascript,Google Api,我正试图从谷歌获取javascript中的日历信息。我读过“操作”手册。他们没有帮忙。即使是这个“有用的”复制粘贴代码(授权)也没有。有人能教我如何使用谷歌api吗?也许有人有一些样品可以分享 这段漂亮的js代码: <html> <button id="authorize-button" onclick='handleAuthClick()'>Authorize</button> <script type="text/javascript">

我正试图从谷歌获取javascript中的日历信息。我读过“操作”手册。他们没有帮忙。即使是这个“有用的”复制粘贴代码(授权)也没有。有人能教我如何使用谷歌api吗?也许有人有一些样品可以分享

这段漂亮的js代码:

<html>
<button id="authorize-button" onclick='handleAuthClick()'>Authorize</button>

<script type="text/javascript">
    var clientId = '***';
    var apiKey = '***';
    var scopes = 'https://www.googleapis.com/auth/plus.me';

    function handleClientLoad() {
        gapi.client.setApiKey(apiKey);
        window.setTimeout(checkAuth,1);
    }

    function checkAuth() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
    }

    function handleAuthResult(authResult) {
        var authorizeButton = document.getElementById('authorize-button');
        if (authResult && !authResult.error) {
            authorizeButton.style.visibility = 'hidden';
            makeApiCall();
        } else {
            authorizeButton.style.visibility = '';
            authorizeButton.onclick = handleAuthClick;
        }
    }

    function handleAuthClick(event) {
        // Step 3: get authorization to use private data
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
        return false;
    }

    // Load the API and make an API call.  Display the results on the screen.
    function makeApiCall() {
        // Step 4: Load the Google+ API
        gapi.client.load('plus', 'v1', function() {
            // Step 5: Assemble the API request
            var request = gapi.client.plus.people.get({
            'userId': 'me'
            });
            // Step 6: Execute the API request
            request.execute(function(resp) {
            var heading = document.createElement('h4');
            var image = document.createElement('img');
            image.src = resp.image.url;
            heading.appendChild(image);
            heading.appendChild(document.createTextNode(resp.displayName));

            document.getElementById('content').appendChild(heading);
            });
        });
    }
</script>

所以我坚持使用“gapi.auth.authorize”。根据您收到的错误,我的猜测是您的Javascript源代码没有在从中获取客户端ID的服务器上正确配置,和/或您试图从文件系统而不是通过web服务器运行脚本,即使是在
localhost
上运行的服务器。Google API客户端,正如我所知,不接受来自文件系统或任何未配置为根据提供的客户端ID请求授权的域的授权请求。

Google API控制台参考:

在web应用程序的客户端ID中:

Javascript源代码:
http://localhost:3000/


浏览器应用程序的关键点:

推荐人:
http://localhost:3000/


localhost可以100%工作

在本地web服务器上运行html文件后,我得到了与您所希望的相同的错误,问题得到了解决

我为web应用程序创建了凭据,并使用“”字符串将以下值设置为本地值

我也检查了json文件。因此,我得到了以下json文件

{"web":
 {
    "client_id":"myClientID",
    "project_id":"my-project",
    "auth_uri":"https://accounts.google.com/o/oauth2/auth",
    "token_uri":"https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
    "client_secret":"XqXmgQGrst4xkZ2pgJh3Omxg",
    "redirect_uris":["http://localhost:5000"],
    "javascript_origins":["http://localhost:5000"]
 }
}


从本地文件查询时,有些API可以正常工作,但有些则不行。 为了响应像您这样的错误,请尝试从web服务器提供您的文件。如果需要快速运行web服务器,请使用Python的内置HTTP服务器(MacOSX和Linux系统预先安装了Python)。此HTTP服务器可以将系统中的任何目录转换为web服务器目录<将code>cd导入项目目录,然后运行以下命令:
python-m SimpleHTTPServer 3000
末尾的数字是http服务器将从中启动的端口号,您可以更改该端口号。在我们的示例中,您的目录将从以下位置提供:
http://localhost:3000

这看起来与我正在使用的代码不完全一样,但我已经让GAPI JS客户端从AngularJS服务内部进行身份验证和查询,所以我不希望它这样做。你到底有什么问题?你走了多远?GAPI正在装货吗?您是否收到要求验证和授权的弹出窗口?如果您
console.log
您的
authResult
,它是否已填充?如果您在
execute
回调中记录
resp
,该怎么办?您是否阻止了Google登录弹出窗口?在控制台中,im getting“未能在“DOMWindow”上执行“postMessage”:提供的目标源('file://')与收件人窗口的源('null')不匹配。因此,im停留在“gapi.auth.authorize”上。我的目标是使用一些静态键来访问我的日历,无论何时何地,我都可以启动javascript代码。谢谢您的回复。我将尝试使用localhost,而不是在了解了什么是JS源代码之后:)返回到您配置项目并获取客户端ID的位置。您应该拥有仍然可以编辑Javascript源代码和重定向URI的字段。来源列表列出了GAPI服务器将从中接受授权请求的主机。如果
http://localhost
不在列表中,则来自
http://localhost
将生成一个与您已经从文件系统获得的错误类似的错误,该错误根本不受支持。是。我在文件系统中使用js。谢谢。我要去试一下本地主机的负载我应该说它帮了我很多。请在服务器上部署相同的html文件。在本地主机上,我仍然有相同的错误。我不完全理解这里的答案。您如何“配置”域以接受授权,以及您的客户端ID是什么?google api控制台不起作用。为了进行调试,如果您安装了Ruby,只需将cd刻录到index.html文件所在的文件夹中,然后运行
Ruby-run-e httpd.
(不要错过该点)这将启动可在
http://localhost:8080/
。似乎有一种方法可以使用CORS在文件系统中工作:如何在加载youtube iframe的html文件中实现这一点?
"Authorized JavaScript origins" 
"Authorized redirect URIs
{"web":
 {
    "client_id":"myClientID",
    "project_id":"my-project",
    "auth_uri":"https://accounts.google.com/o/oauth2/auth",
    "token_uri":"https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
    "client_secret":"XqXmgQGrst4xkZ2pgJh3Omxg",
    "redirect_uris":["http://localhost:5000"],
    "javascript_origins":["http://localhost:5000"]
 }