use*_*145 18 javascript google-api
我想在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>
Run Code Online (Sandbox Code Playgroud)
错误消息(来自控制台):
'Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('file://') does not match the recipient window's origin ('null').'
Run Code Online (Sandbox Code Playgroud)
所以我坚持'gapi.auth.authorize'.什么都不起作用
cit*_*ave 28
根据您收到的错误,我猜您要么在您从中获取客户端ID 的Google API控制台上未正确配置Javascript Origin ,并且/或者您尝试从文件系统运行脚本而不是通过Web服务器,甚至一个运行localhost
.我能说的Google API客户端不接受来自文件系统或未配置为根据提供的客户端ID请求授权的任何域的授权请求.
归档时间: |
|
查看次数: |
23532 次 |
最近记录: |