Javascript中的Google API [英] Google API in Javascript

查看:180
本文介绍了Javascript中的Google API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从javascript中获取Google的日历信息。我已阅读'如何'手册。他们没有帮助。即使这个'有用'的copypasted代码(授权)也没有。有人会教我如何使用谷歌API?也许有人有一些样品可以分享



以及这个美丽的js代码:

 < HTML> 
< button id =authorize-buttononclick ='handleAuthClick()'>授权< /按钮>

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

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

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

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



函数handleAuthClick(event){
//第3步:获得使用私有数据的授权
gapi.auth.authorize({ client_id:clientId,scope:scopes,immediate:false},handleAuthResult);
返回false;
}

//载入API并进行API调用。在屏幕上显示结果。
函数makeApiCall(){
//第四步:加载Google+ API
gapi.client.load('plus','v1',function(){
//步骤5:汇编API请求
var request = gapi.client.plus.people.get({$ b $'userId':'me'
});
// Step 6:执行API请求
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>



<错误信息(来自控制台):

 ''DOMWindow'无法执行'postMessage':提供的目标原点'file://')与收件人窗口的原点('null')不匹配。'

所以我坚持'gapi.auth.authorize'。根据你收到的错误,我的猜测是,你要么没有配置好你的Javascript Origin(原始数据源)正确地在 Google API控制台上获得您的客户端ID,和/或您试图从文件系统运行脚本而不是通过Web服务器,甚至是在 localhost 上运行的服务器。据我所知,Google API客户端不接受来自文件系统或尚未配置为根据提供的客户端ID请求授权的任何域的授权请求。


I am trying to get calendar info from google in javascript. I ve read 'how to' manuals. They didn't help. Even this 'helpful' copypasted code (to authorize) didn't. Would somebody be so kind to teach me how to use google api? Maybe someone has some samples to share

And this beautiful js code :

<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>

Error Message (from Console):

 'Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('file://') does not match the recipient window's origin ('null').'

so im stuck on 'gapi.auth.authorize'. nothing works after

解决方案

Based on the error you're receiving, my guess is that you either do not have your Javascript Origin configured properly on the Google API console you got your Client ID from, and/or you are trying to run your script from the file system instead of through a web server, even one running on localhost. The Google API client, near as I've been able to tell, does not accept authorization requests from the file system or any domain that has not been configured to request authorization under the supplied Client ID.

这篇关于Javascript中的Google API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆