【问题标题】:Youtube Data API and Google Cloud EndpointsYoutube 数据 API 和谷歌云端点
【发布时间】:2016-01-20 08:17:01
【问题描述】:

我在让 Google 云端点与我的 javascript 客户端中的 YouTube 数据 API v3 协同工作时遇到问题。我认为我的问题在于 gapi.client.setApiKey() 方法为我的端点 API 和 YouTube api 设置密钥。当我设置密钥 YouTube 工作但我的端点没有,我使用我的端点 API 看到以下错误:

{
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured. The API () is not enabled for your project. Please use the Google
 Developers Console to update your configuration.",
    "extendedHelp": "https://console.developers.google.com"
}

如果没有密钥,我的端点可以工作,但 youtube 搜索不能,我使用搜索功能收到此消息:

{
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
}

加载 API 的代码总结如下,但基本上我遵循了端点 python/javascript 教程和 youtube 数据 API 教程!

init = function(apiRoot) {
  var apisToLoad;

  var callback = function(){
    if(--apisToLoad == 0){
      enableButtons();
    }
  }

  apisToLoad = 2; // must match number of calls to gapi.client.load()
  gapi.client.load('myAPIName', 'v1', callback, apiRoot);
  gapi.client.load('youtube', 'v3', onYouTubeApiLoad); 
};

// Called automatically when YouTube API interface is loaded (see line 9).
function onYouTubeApiLoad() {
    //sets the api key
    gapi.client.setApiKey('APIKeyForYouTubeFromDevConsole');
}

【问题讨论】:

  • 你能在你的处理程序在这里做的事情中提供更多的代码吗?我知道可以做到,而且我知道对端点和 youtube 使用相同的密钥是错误的,但是如果没有更多代码,很难告诉您需要更改的内容
  • 更新了一些代码,如果有帮助,请告诉我...我想我需要指定要为哪个 API 设置密钥,但不知道该怎么做?谢谢!

标签: javascript python google-cloud-endpoints google-cloud-platform youtube-data-api


【解决方案1】:

要仅使用 API 密钥验证 youtube API 请求,请删除 api.client.setApiKey 方法调用。

在对 YouTube 数据 API 的调用中,向 API 请求添加一个关键参数:

var request = gapi.client.youtube.search.list({
    part: 'snippet',
    type: 'video',
    maxResults: 12,
    q: searchValue,
    key: 'YourAPIKeyGoesHere'
});

这意味着只有这些 API 调用被授权,而不是端点调用。

【讨论】:

    【解决方案2】:

    我对 Youtube Data API 不是很熟悉。但我承认您用于端点的代码是我们提供的代码。您绝对可以将此代码用于 Endpoints API。对于 Youtube Data 一,我建议looking here

    看起来你需要的代码是这样的:

    import com.google.api.client.auth.oauth2.Credential;
    import com.google.api.services.youtube.YouTube;
    
    public class myClass {
    
        /**    
         * Define a global instance of a Youtube object, which will be used
         * to make YouTube Data API requests.
         */
        private static YouTube youtube;
    
        public static void main(String[] args) {
    
            List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
    
            try {
                // Authorize the request.
                Credential credential = Auth.authorize(scopes, "invideoprogramming");
    
                // This object is used to make YouTube Data API requests.
                youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
                    .setApplicationName([YOUR APP])
                    .build();
            }
    

    从那里您应该能够使用 youtube 对象进行调用,并使用 gapi 将内容发送到您的端点。

    【讨论】:

    • 感谢您的帮助 = 链接指向的东西给了我答案,我会在一分钟后将其作为新答案发布。
    猜你喜欢
    • 2015-10-27
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多