Google API: Not a valid origin for the client: url has not been whitelisted for client ID “ID”

前端 未结 17 1595
北海茫月
北海茫月 2020-12-23 14:34

I need help. I don\'t found an answer to my question. I tried googling and I tried asking on other sides but I never found an answer.

I\'m working with the google API

17条回答
  •  独厮守ぢ
    2020-12-23 14:55

    I just made the same mistake: Tried the official quickstart example and received the same error message as you.

    It is rather confusing, because that example is a lot more complex than what I personally needed: It uses OAuth for user login, and NOT just the API key. If you are like me, and you don't want to use OAuth, and you only want to retrieve some Youtube data, without any privileged actions (e.g. if you only want to search or list videos, channels or playlists), this example is for you.

    The solution is simple, just provide apiKey instead of clientId to gapi.client.init (link: API docs), like so:

    const apiKey = '';
    
    function gooApiInitClient() {
      // Array of API discovery doc URLs for APIs used by the quickstart
      const discoveryDocs = [
        "https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest"
      ];
    
      return gapi.client.init({
        apiKey,
        discoveryDocs
      });
    }
    
    // see: https://developers.google.com/api-client-library/javascript/reference/referencedocs
    gapi.load('client', {
      callback: function() {
        // we now have gapi.client! initialize it.
        gooApiInitClient().
          then(() => {
            // we can start using the API here!
            // e.g. gapi.client.youtube.videos.list(...);
          }).then(results => { 
            // use results here....
          });
      }
    });
    

提交回复
热议问题