如何解决Google 驱动器文件选择器:点击“选择”时对话框不会关闭
我知道这已经被问过很多次了,我浏览了无数关于这个问题的线程,但似乎没有解决方案适用于我的情况。使用谷歌驱动器文件选择器,在选择过程中,我始终在控制台中收到此错误:
无法在“DOMWindow”上执行“postMessage”:目标来源 提供的 ('https://docs.google.com') 与收件人不匹配 窗口的来源 ('https://develop.uninettunouniversity.net')
尽管出现错误,选择器第一次工作正常,但如果我第二次打开它,选择一个文件并单击“选择”按钮不会关闭对话框。在选择的文件返回到回调后,要关闭对话框,用户必须点击右上角的“取消”或“x”按钮。我不确定控制台错误和选择器对话框问题是否相关。
我已阅读所有这些内容:
- The target origin provided ('https://docs.google.com') does not match the recipient window's origin ('http://localhost:3000')
- Google API error,but still works
- Failed to execute 'postMessage' on 'DOMWindow' using Google code
- Failed to execute 'postMessage' on 'DOMWindow': https://www.youtube.com !== http://localhost:9000
- Google Picker refuses to load with error "Failed to execute 'postMessage' on 'DOMWindow': The target origin provided"
我是谷歌谷歌驱动器文件选择器和谷歌驱动器 API,这是源代码,它几乎“按原样”取自谷歌驱动器选择器文档中的在线示例:
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
<script>
// The browser API key obtained from the Google Api Console.
// Replace with your own browser API key,or your own key.
var developerKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
// The Client ID obtained from the Google Api Console. Replace with your own Client ID.
var clientId = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
var appId = "xxxxxxxxxxxxxxxx";
// Scope to use to access user's Drive items.
var scope = ['https://www.googleapis.com/auth/drive.file'];
var pickerApiLoaded = false;
var oauthToken;
// Use the Google Api Loader script to load the google.picker script.
function loadPicker() {
gapi.load('auth',{ 'callback': onAuthApiLoad });
gapi.load('picker',{ 'callback': onPickerApiLoad });
}
function onAuthApiLoad() {
window.gapi.auth.authorize(
{
'client_id': clientId,'scope': scope,'immediate': false
},handleAuthResult);
}
function onPickerApiLoad() {
pickerApiLoaded = true;
createPicker();
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}
// Create and render a Picker object for searching images.
function createPicker() {
if (pickerApiLoaded && oauthToken) {
var view = new google.picker.View(google.picker.ViewId.DOCS);
view.setMimeTypes("image/png,image/jpeg,image/jpg");
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.setAppId(appId)
.setoAuthToken(oauthToken)
.addView(view)
.addView(new google.picker.DocsuploadView())
.setDeveloperKey(developerKey)
.setCallback(pickerCallback)
.build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
var fileId = data.docs[0].id;
alert('The user selected: ' + fileId);
}
}
</script>
<div>
<button type="button" onclick="loadPicker()">Load google drive picker</button>
</div>
-
授权的 javascript 源和重定向 uri 包含在 google 控制台中,所有 uri 都在 SSL 下。
-
我已启用选择器和谷歌驱动器 API
-
我试图在没有效果的情况下注释掉 setDeveloperKey() 和 setAPPid()
-
范围包含在开发者控制台 oauth 同意屏幕部分
-
我没有收到上述许多问题中常见的 cors 错误或“无效的 x-frame-options 标头”
-
我尝试在加载 API 时包含 API 密钥作为参数,例如:
src="https://apis.google.com/js/api.js?key=xxxxxxxxxxxxxxxxxxx"
-
源由本地 html 服务器 (IIS) 提供服务,我的生产服务器上也有同样的问题。两者都在 SSL 下。
-
我已经按照文档中的建议尝试了 picker.setorigin(window.location.protocol + '//' + window.location.host )
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。