Google Drive API无法通过JS打开标准共享对话框(x-frame-options error) [英] Google Drive API, can't open standard sharing dialog via JS (x-frame-options error)

查看:99
本文介绍了Google Drive API无法通过JS打开标准共享对话框(x-frame-options error)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Google Drive API的JavaScript应用程序。我阅读了如何在此处打开标准共享对话框: https://developers.google.com/ drive / web / manage-sharing

 < head> 
...
< script type =text / javascriptsrc =https://apis.google.com/js/api.js>< / script>
< script type =text / javascript>
init = function(){
s = new gapi.drive.share.ShareClient('< MY_APP_ID>');
s.setItemIds([< MY_FILE_ID>]);

window.onload = function(){
gapi.load('drive-share',init);
}
< / script>
< / head>
< body>
< button onclick =s.showSettingsDialog()>分享< /按钮>
< / body>

好像我做的都是正确的,当我点击 share 按钮,对话框开始加载,但无法加载。



在控制台中,我看到:

 拒绝在框架
中显示https://drive.google.com/share?...,因为它设置了X-Frame-Options到'SAMEORIGIN'。

我搜索了这个错误,我发现在SO和其他一些类似的问题中网站,但他们没有帮助。我猜Google不允许自己进入非谷歌网站的框架(导致SAMEORIGIN)。



我可以做些什么来打开共享在您的应用中启动Google云端硬盘共享对话框页面 //developers.google.com/drive/web/manage-sharingrel =nofollow> here 指出:


启动对话框的页面的URL必须与为该应用注册的打开URL具有相同的原始


然后,如果您查看配置Drive SDK的说明,请此处,您可以看到打开网址部分显示为:


关于Open URL需要注意两点:




  • 确保您为Open URL提供完全合格的域名 - localhost won将不起作用。

  • URL必须是属于你的。应用注册完成后,您需要验证您对此网址的所有权,才能创建一个
    的Chrome网上应用店列表。有关详细信息,请参阅网站验证。


因此,启动对话框的页面 与Google Drive SDK设置中为应用注册的打开网址具有相同的来源。因此,要解决您的问题,请更正打开网址,以便它具有正确的网址,即具有与打开网址相同来源的网址。请注意,您可以通过 https://console.developers.google.com/project 更改Google Drive SDK设置。 / a>。



以及确保Open URL设置正确。您还需要将您的云端硬盘SDK应用ID替换为MY_APP_ID。您可以按照以下说明找到应用程序ID:


  1. 转到 https://console.developers.google.com

  2. 点击您的项目

  3. 点击API和auth在左侧

  4. 点击Drive SDK设置cog图标

  5. 然后可以在Google Drive SDK标题,例如应用程序ID:47XXXXXXXX3


I have a JavaScript app which uses the Google Drive API. I read how to open a standard sharing dialog here: https://developers.google.com/drive/web/manage-sharing

<head>
...
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
<script type="text/javascript">
    init = function() {
        s = new gapi.drive.share.ShareClient('<MY_APP_ID>');
        s.setItemIds(["<MY_FILE_ID>"]);
    }
    window.onload = function() {
        gapi.load('drive-share', init);
    }
</script>
</head>
<body>
    <button onclick="s.showSettingsDialog()">Share</button>
</body>

Seems like I do everything right, when I click my share button, the dialog starts loading but it can't be loaded.

In the console I see:

Refused to display 'https://drive.google.com/share?...' in a frame
because it set 'X-Frame-Options' to 'SAMEORIGIN'.

I've googled this error and I've seen that there are some similar questions in SO and other sites, but they don't help. I guess Google doesn't allow itself to be in a frame in a not-google-site (cause of "SAMEORIGIN").

What can I do to open sharing dialog in my app?

解决方案

The "Launching the Google Drive sharing dialog in your app" page here states:

The URL of the page that launches the dialog must have the same origin as the Open URL registered for the app.

If you then look at the instructions to "Configure the Drive SDK" here, you can see that the "Open URL" section reads:

There are two important things to keep in mind for the Open URL:

  • Make sure you give a fully qualified domain name for Open URL -- localhost won't work.
  • The URL must belong to you. After the app registration is complete, you'll need to verify your ownership of this URL in order to create a Chrome Web Store listing. For more information, see Site Verification.

Hence your page which is launching the dialog does not have the same origin as the Open URL registered for the app in you Google Drive SDK settings. So to fix your problem correct the Open URL so that it has the correct URL i.e. a URL with the same origin as the Open URL. Note that you can change the Google Drive SDK settings via https://console.developers.google.com/project.

As well as making sure the Open URL is set correctly. You'll also need to substitute your Drive SDK app ID for 'MY_APP_ID'. You can find the App ID by following these instructions:

  1. Goto https://console.developers.google.com
  2. Click your project
  3. Click "APIs and auth" on the left
  4. Click the "Drive SDK" settings cog icon
  5. The "App ID" can then be found under the "Google Drive SDK" title e.g. App ID: 47XXXXXXXX3

这篇关于Google Drive API无法通过JS打开标准共享对话框(x-frame-options error)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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