2014-09-25 81 views
0

我想查看我的頻道視頻列表,並且出現了這個錯誤invalid_client無法訪問OAuth2 Youtube API V3

未找到OAuth客戶端。這意味着客戶端ID是錯誤的!

var OAUTH2_CLIENT_ID = '############'; 
var OAUTH2_SCOPES = ['https://www.googleapis.com/auth/youtube'];` 

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

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

function handleAuthResult(authResult) { 
    if (authResult && !authResult.error) { 
     $('.pre-auth').hide(); 
     $('.post-auth').show(); 
     loadAPIClientInterfaces(); 
    } else { 
     $('#login-link').click(function() { 
      gapi.auth.authorize({ 
       client_id: OAUTH2_CLIENT_ID, 
       scope: OAUTH2_SCOPES, 
       immediate: false 
      }, handleAuthResult); 
     }); 
    } 
} 

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

的console.google的截圖:

enter image description here

回答

0

你並不需要連接或使用的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({ 
     part: id, snippet 
     type: 'video', 
     order: 'date' 
    }); 
    request.execute(function(response) { 
     //list of all your video in the response 

    }); 
} 

不要忘了這個文件添加到您的index.html後添加此以下行:

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

從DOC YouTube API