我是否需要提交以下 google 驱动器范围的验证请求才能从选择器下载文件?
Do i require to Submit a verfication request for the following google drive scopes in order to download a file from picker?
我是否需要提交以下 google 驱动器范围的验证请求才能从选择器下载文件?
- googleapis.com/auth/drive
- www.googleapis.com/auth/drive.readonly
由于以下错误,选取器未加载
我的 objective 是从 google 驱动器下载一个文件到我们的网站。
当我使用 drive.file 时选择器加载,但是当我尝试下载文件时出现 404 文件未找到错误。
在该网站上使用 google 驱动器参考 files/get 我可以模拟 404 但如果我将范围更改为仅使用驱动器或 drive.readonly 我会得到 200 状态我在找什么,但选择器没有加载。
有没有无需提交验证申请表即可解决此问题的方法?
我使用的代码基于 google 驱动示例代码。
<!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 Developers Console.
var developerKey = '';
// The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
var clientId = ""
// Scope to use to access user's photos.
var scope = ['https://www.googleapis.com/auth/drive'];
var pickerApiLoaded = false;
var oauthToken;
// Use the API Loader script to load google.picker and gapi.auth.
function onApiLoad() {
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 picking user Photos.
function createPicker() {
var view = new google.picker.DocsView();
view.setIncludeFolders(true);
if (pickerApiLoaded && oauthToken) {
var picker = new google.picker.PickerBuilder().
addView(view).
setOAuthToken(oauthToken).
setDeveloperKey(developerKey).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
debugger;
var url = 'nothing';
var id = "";
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
id = doc[google.picker.Document.ID];
}
var accessToken = gapi.auth.getToken().access_token;
var xhr = new XMLHttpRequest();
var url = "https://www.googleapis.com/drive/v3/files/" + id + "?alt=media";
xhr.open('GET', url);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.send();
//var message = 'You picked: ' + url;
//document.getElementById('result').innerHTML = message;
}
</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=onApiLoad"></script>
</body>
</html>
首先,我建议使用 https://www.googleapis.com/auth/drive 以获得完整范围。然后,给我们看看代码是什么,你是怎么做到的?
您没有在变量选择器中设置范围
var picker = new google.picker.PickerBuilder().
addView(view).
addView(google.picker.ViewId.DOCS).//<== add this
我是否需要提交以下 google 驱动器范围的验证请求才能从选择器下载文件?
- googleapis.com/auth/drive
- www.googleapis.com/auth/drive.readonly
由于以下错误,选取器未加载
我的 objective 是从 google 驱动器下载一个文件到我们的网站。
当我使用 drive.file 时选择器加载,但是当我尝试下载文件时出现 404 文件未找到错误。
在该网站上使用 google 驱动器参考 files/get 我可以模拟 404 但如果我将范围更改为仅使用驱动器或 drive.readonly 我会得到 200 状态我在找什么,但选择器没有加载。
有没有无需提交验证申请表即可解决此问题的方法?
我使用的代码基于 google 驱动示例代码。
<!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 Developers Console.
var developerKey = '';
// The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
var clientId = ""
// Scope to use to access user's photos.
var scope = ['https://www.googleapis.com/auth/drive'];
var pickerApiLoaded = false;
var oauthToken;
// Use the API Loader script to load google.picker and gapi.auth.
function onApiLoad() {
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 picking user Photos.
function createPicker() {
var view = new google.picker.DocsView();
view.setIncludeFolders(true);
if (pickerApiLoaded && oauthToken) {
var picker = new google.picker.PickerBuilder().
addView(view).
setOAuthToken(oauthToken).
setDeveloperKey(developerKey).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
debugger;
var url = 'nothing';
var id = "";
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
id = doc[google.picker.Document.ID];
}
var accessToken = gapi.auth.getToken().access_token;
var xhr = new XMLHttpRequest();
var url = "https://www.googleapis.com/drive/v3/files/" + id + "?alt=media";
xhr.open('GET', url);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.send();
//var message = 'You picked: ' + url;
//document.getElementById('result').innerHTML = message;
}
</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=onApiLoad"></script>
</body>
</html>
首先,我建议使用 https://www.googleapis.com/auth/drive 以获得完整范围。然后,给我们看看代码是什么,你是怎么做到的?
您没有在变量选择器中设置范围
var picker = new google.picker.PickerBuilder().
addView(view).
addView(google.picker.ViewId.DOCS).//<== add this