我正在尝试遵循在独立脚本中使用文件选择器api的说明。 现在,我只想让“ Hello World”示例正常工作
google文档中有两个不同的来源。我最终使用了这个: https://developers.google.com/drive/api/v3/picker
我所做的是将html代码复制并粘贴到脚本项目上的html文件中,并使code.gs看起来像这样(如Script Apps Guide的HTML Service部分所述):
function doGet() {
return HtmlService.createHtmlOutputFromFile('index');
}
然后,我通过资源> Cloud Platform Project在控制台中创建了凭据。我从控制台复制了developerKey,clientId和appId到我的代码中。然后,我进行了“版本管理”和“发布>部署为Webapp”位。 当我尝试URL时,出现该错误: redirect_uri_mismatch
此后,我开始搜索并找到此线程:
Google Apps Script - Using Google's file picker on a standalone script 根据建议,我添加了
.setOrigin(google.script.host.origin)
到pickerBuilder定义的行。我已经更新了版本,再次发布,但是仍然出现相同的错误。
所以现在html代码如下(我更改了个人资料):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Picker Example</title>
<script type="text/javascript">
// The Browser API key obtained from the Google API Console.
// Replace with your own Browser API key, or your own key.
var developerKey = 'AIzaSyDZzfmUYVhy112mmvMdGjyxu87NzKPRw2g';
// The Client ID obtained from the Google API Console. Replace with your own Client ID.
var clientId = "920142203274-3v3laj3p4hlvufugi6lq1r6oehal8umj.apps.googleusercontent.com"
// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
var appId = "920142203274";
// Scope to use to access user's Drive items.
var scope = ['https://www.googleapis.com/auth/drive'];
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)
.setOrigin(google.script.host.origin)
.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>
</head>
<body>
<div id="result"></div>
<!-- The Google API Loader script. -->
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=loadPicker"></script>
</body>
</html>
错误是一样的,看起来像这样:
由于找不到特定用例的指南,因此我使用的是 https://developers.google.com/apps-script/guides/dialogs#file-open_dialogs 和来自的代码 https://developers.google.com/drive/api/v3/picker 所以我不确定它是否真的可以那样工作。如果能找到正确的指南(如果有的话)并进行研究,我将非常高兴。
谢谢大家。