在this page上有一个示例,说明如何在普通的JavaScript网页中包含Google Drive Picker。我已将示例代码复制并粘贴到我的计算机上的文件中,然后按照说明使用我自己的API密钥,客户端ID和应用程序ID替换示例字符串。我在做什么错误的Google Drive Picker示例代码?
如果我使用python -m SimpleHTTPServer 8000
从本地主机提供页面并访问该页面,它将以两种方式失败。首先,弹出的Drive对话框显示“出现错误!API开发人员密钥无效。”其次,在控制台上,我看到下面的错误。搜索网络告诉我,这可能是因为我使用的是非HTTPS连接,Google API会尝试与驱动器建立HTTPS连接。 (相反的是他们的错误好像说。)
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://docs.google.com') does not match the recipient window's origin ('http://localhost:8000').
(anonymous) @ cb=gapi.loaded_0:46
Uncaught ReferenceError: init is not defined
at onload (https://docs.google.com/picker?protocol=gadgets&origin=http%3A%2F%2Flocalho…22%7D))&rpctoken=6pstmf1ss7m2&rpcService=v8gvn97850jj&thirdParty=true:3:54)
如果我所服务从本地主机使用this handy python script for an SSL web server页面,访问的页面,它以不同的方式失败。弹出的驱动器对话框显示以下内容。
400. That’s an error.
Error: origin_mismatch
Request Details
proxy=oauth2relay379676703
immediate=false
scope=https://www.googleapis.com/auth/drive
origin=https://localhost:8000
response_type=token
redirect_uri=postmessage
state=912029351|0.3907265328
client_id=[REDACTED]
include_granted_scopes=true
jsh=m;/_/scs/apps-static/_/js/k=oz.gapi.en.eBn7I_cE2GI.O/m=__features__/am=AQ/rt=j/d=1/rs=AGLTcCNXKKlDNVVUdz0bvaxNh7fYe-hpSQ
gsiwebsdk=1
That’s all we know.
尽管这似乎表明,驱动器API不希望从本地主机听,我必须设置为接受从https://localhost:8000/*
和http://localhost:8000/*
请求的应用程序的凭据(在我的谷歌开发者信息中心)。
我在做什么错?
我不能使用URL shortener方法,因为我的URL包含一个goo.gl不支持的端口。我按照链接问题中的建议编辑了我的/ etc/hosts文件,并且现在从本地加载了假的URL,因为它应该是这样,但是行为与如果我简单地使用localhost相反会发生什么。换句话说,我原来的问题中的所有错误依然存在;从URL中删除“localhost”我没有任何改进。 – Nathan
@Nathan你能提供编辑hosts文件后看到的错误吗? –
当我尝试通过URL mydevserver.example.com访问本地主机时,原始帖子中出现的两个错误中的第二个错误是由/ etc/hosts重新路由到本地主机。我已尝试使用http和https,在您最后评论后不久将mydevserver.example.com添加到我的Google API凭证中,然后等待30分钟以确保其生效。 – Nathan