Google Storage API - 为什么gapi.client.storage未定义?

时间:2016-06-29 19:27:57

标签: angularjs google-cloud-storage gapi

我是Google云端存储新手,我正在尝试创建一个Angular服务来执行Google Storage API调用。当我尝试列出存储桶时,出现错误:

"Error: gapi.client.storage is undefined"

如何确保云存储API已加载并正常运行?

从控制器调用

var apiKey = 'MY API KEY';
var projectId = 'MY PROJECT ID FROM GOOGLE STORAGE';
var clientId = 'MY CLIENT ID FROM GOOGLE STORAGE'

googleCloudService.handleClientLoad(apiKey); 
googleCloudService.initializeApi('v1'); 
googleCloudService.listBuckets(projectId); // <- Throws the Console Errror 

googleCloudService:

/**
 * Set required API keys and check authentication status.
 */
this.handleClientLoad = function(apiKey) {
  gapi.client.setApiKey(apiKey);
};

/**
 * Load the Google Cloud Storage API.
 */
this.initializeApi = function(apiVersion){
  gapi.client.load('storage', apiVersion).then(function success(res) {
      console.log('loaded storage api');
    },
    function error(res) {
      return alert('Error loading storage api: ' + JSON.stringify(res)); // throw error
    });
};

/**
 * Google Cloud Storage API request to retrieve the list of buckets in
 * your Google Cloud Storage project.
 */
this.listBuckets = function(projectId) {
  var request = gapi.client.storage.buckets.list({  // <-- ERROR
    'project': projectId
  });
  console.log(request);
};

0 个答案:

没有答案