问题
I am working on Google Drive picker in my web application. Everything works fine but when it opens an Authentication window and I approved it, the window automatically close without showing or allowing file selection.
Here is my JavaScript code:
// The Browser API key obtained from the Google API Console. var developerKey = '*****************************';
// The Client ID obtained from the Google API Console. Replace with your own Client ID.
var clientId = '*********-k826p6sl1qkhpnrtbbi0jktboijupvf9.apps.googleusercontent.com';
// Scope to use to access user's photos.
var scope = 'https://www.googleapis.com/auth/drive.readonly';
var pickerApiLoaded = false;
var oauthToken;
// Use the API Loader script to load google.picker and gapi.auth.
function onApiLoad() {
gapi.load('auth2', onAuthApiLoad);
gapi.load('picker', onPickerApiLoad);
}
function onAuthApiLoad() {
var authBtn = document.getElementById('GoogleDrive');
authBtn.disabled = false;
authBtn.addEventListener('click', function () {
gapi.auth2.authorize({
client_id: clientId,
scope: scope
}, 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() {
if (pickerApiLoaded && oauthToken) {
var picker = new google.picker.PickerBuilder().
addView(google.picker.ViewId.PHOTOS).
setOAuthToken(oauthToken).
setDeveloperKey(developerKey).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
var url = 'nothing';
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
}
var message = 'You picked: ' + url;
document.getElementById('result').innerHTML = message;
}
I am using below reference:
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
Can anyone help me out? Thanks in advance.
来源:https://stackoverflow.com/questions/49734865/google-drive-close-automatically-after-authentication