最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Drive API, can't open standard sharing dialog via JS (x-frame-options error) - Stack Overflow

programmeradmin7浏览0评论

I have a JavaScript app which uses the Google Drive API. I read how to open a standard sharing dialog here:

<head>
...
<script type="text/javascript" src=".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 '?...' 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?

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

<head>
...
<script type="text/javascript" src="https://apis.google./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./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?

Share Improve this question edited Nov 3, 2014 at 0:11 Ben Smith 20.3k6 gold badges73 silver badges97 bronze badges asked Mar 12, 2014 at 6:53 imkostimkost 8,1637 gold badges32 silver badges49 bronze badges 2
  • It would help if you include the code you are using to try to open the standard sharing dialog. – Ben Smith Commented Mar 14, 2014 at 16:02
  • 1 @Fresh, included the code – imkost Commented Mar 15, 2014 at 16:04
Add a ment  | 

2 Answers 2

Reset to default 4 +100

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 plete, 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./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.
  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

The problem was solved thanks to this answer https://stackoverflow./a/20742994/1185123

dan-man says in his answer:

Client ID You can find this in the Google Cloud Console - see above. Mine is a 12 digit number, so yours will probably be too.

Mine client id looks like

175564412906-ui22fsaghkvkkj09j2bprku55m8k3d0d.apps.googleusercontent.

I've used this id in

s = new gapi.drive.share.ShareClient('<MY_APP_ID>');

After reading the answer, I tried to use only first 12 digits of my client id. I didn't expect it to work, I was just desperate. But the strange thing, it works perfectly!

If somebody can explain why it works and why nothing about this is said in the documentation — you are wele to answer!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论