Google API客户端ID无权授权

时间:2014-09-23 19:13:25

标签: javascript jquery youtube google-api youtube-api

我正在尝试创建一个应用程序来对youtube视频进行基本搜索。我使用的是Youtube Data API,然后我访问了Google开发人员的控制台,并为我的域名创建了一个客户端ID。

我下载了auth.js和Google在其示例代码部分中的search.js,并将我的客户ID放入其所说的“我的客户ID”中。但申请没有成功。我使用了console.log,似乎我没有超越函数check'uth'。

我错过了什么吗?以下是该页面的链接:http://www.vidme.cassandraburnscreative.com/#search

这是auth.js和search.js一起

var OAUTH2_CLIENT_ID = 'my client ID';
var OAUTH2_SCOPES = [
  'https://www.googleapis.com/auth/youtube'
];


googleApiClientReady = function() {
  console.log("googleApiClientReady");
  gapi.auth.init(function() {
    window.setTimeout(checkAuth, 1);
    console.log("gapi.auth.init");
  });
}


function checkAuth() {
  gapi.auth.authorize({
    client_id: OAUTH2_CLIENT_ID,
    scope: OAUTH2_SCOPES,
    immediate: true
  }, handleAuthResult);
  console.log("checkAuth");
}


function handleAuthResult(authResult) {
  if (authResult && !authResult.error) {
    // Authorization was successful. Hide authorization prompts and show
    // content that should be visible after authorization succeeds.
    $('.pre-auth').hide();
    $('.post-auth').show();
    loadAPIClientInterfaces();
    console.log("Load Interfaces");
  } else {

    $('#login-link').click(function() {
      console.log("nope");
      gapi.auth.authorize({
        client_id: OAUTH2_CLIENT_ID,
        scope: OAUTH2_SCOPES,
        immediate: false
        }, handleAuthResult);
      console.log("HandleAuthResult");
    });
  }
}

function loadAPIClientInterfaces() {
  gapi.client.load('youtube', 'v3', function() {
    handleAPILoaded();
    console.log("handleAPILoaded");
  });
}

function handleAPILoaded() {
  $('#search-button').attr('disabled', false);
}

function search() {
  var q = $('#query').val();
  var request = gapi.client.youtube.search.list({
    q: q,
    part: 'snippet'
  });

  request.execute(function(response) {
    var str = JSON.stringify(response.result);
    $('#search-container').html('<pre>' + str + '</pre>');
  });
}

和html

<div class="wrapper">

<div id="buttons">
<p>Search For an Artist:</p>
      <label> <input id="query" placeholder='+ Add Artist' type="text"/>
      <button id="search-button" disabled onclick="search()">Search</button>
      </label>
</div>
    <div id="search-container">
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

您无需连接或使用Oauth2搜索YouTube API。 您只需api key

示例示例:

function googleApiClientReady() {
    var apiKey = 'YOUR_API_KEY';

    gapi.client.setApiKey(apiKey);
    gapi.client.load('youtube', 'v3', function() {
        isLoad = true;
    }); 

    request = gapi.client.youtube.search.list({
        q: q,
        part: 'snippet'
     });
    request.execute(function(response) {
        //your code to here
    });
}

不要忘记将此文件添加到index.html并在以下行之后添加以下行:

<script src="https://apis.google.com/js/client.js?onload=googleApiClientReady"></script>

来自doc YouTube API