我想在我的应用程序中集成 Google 驱动器选择器 api。但是 google picker api 的 onload 函数没有调用。下面是我的代码。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>GoogleDriveApi</title>
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</head>
<body>
<div>
</div>
<script type="text/javascript">
/**************************************************************** start of Google api code ************************************************************************************************/
// The API developer key obtained from the Google Developers Console.
var developerKey = 'AIzaSyAZjLxjshv3FpkLK6517tiZ2nYwt6rJZHk';
// The Client ID obtained from the Google Developers Console.
var clientId = '814476538527-pijtu7ck89ofep1j4tqr2j38ltruahtd.apps.googleusercontent.com';
// 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() {
console.log("load function calledd");
gapi.load('auth', { 'callback': onAuthApiLoad });
gapi.load('picker', { 'callback': onPickerApiLoad });
}
function onAuthApiLoad() {
console.log("auth api success");
window.gapi.auth.authorize(
{
'client_id': clientId,
'scope': scope,
'immediate': false
},
handleAuthResult);
}
function onPickerApiLoad() {
console.log("picker load");
pickerApiLoaded = true;
createPicker();
}
function handleAuthResult(authResult) {
console.log("authressult");
console.log(authResult);
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}
// Create and render a Picker object for picking user Photos.
function createPicker() {
console.log("create picker");
if (pickerApiLoaded && oauthToken) {
var picker = new google.picker.PickerBuilder().
addView(google.picker.DocsUploadView()).
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;
}
/**************************************************************** end of Google api code ************************************************************************************************/
</script>
上面的 .cshtml 页面会在点击页面中的按钮时被调用
$("#button").click(function(){
window.location.href = "/Contact/GoogleApi";
});
控制器代码
public ActionResult GoogleApi(){
retunr View("GoogleApi");
}
GoogleApi.cshmtl 页面正在加载,但没有调用 ononApiLoad
函数。我已将 GooglePickerApi 设置为 On。任何人都可以知道这有什么问题。