Google picker api没有进入onApiLoad功能

时间:2014-06-21 06:04:41

标签: javascript google-api google-drive-api google-picker

我想在我的应用程序中集成G​​oogle驱动器选择器api。但谷歌选择器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页面正在加载,但它没有调用onApiLoad函数。我已将GooglePickerApi设置为On。任何人都可以知道这有什么不对。

1 个答案:

答案 0 :(得分:0)

使用脚本:

  script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"> 
身体而不是头部为我工作。但是,不知道确切的原因。希望你可能已经想到它,因为这个帖子大约是6个月大。如果你现在如何执行这些脚本请告诉我!执行后我指的是执行顺序!