YouTube api v3错误:尝试使用CORS时没有'Access-Control-Allow-Origin'标题

时间:2014-05-02 02:05:16

标签: javascript youtube-api google-api cors

我正在尝试将视频移动到播放列表,当我在YouTube API中进行测试时,它可以正常工作但是当我尝试在我的代码中使用它时,却没有。此访问令牌的获取方式与Ibrahim先生的CORS Upload sample相同,并根据需要添加了范围。

我无法弄清楚我哪里出错了。这是我的ajax代码:

$.ajax({
        url:'https://www.googleapis.com/youtube/v3/playlistItems',
        method: 'POST',
        headers:{
            contentType: 'application/json', //I've tried application/jsonp
            Authorization: 'Bearer ' + accessToken,
            'X-JavaScript-User-Agent':'Google APIs Explorer'
            //I've also tried 'Access-Control-Allow-Origin' : '*'
        },
        data:{
            'kind': 'youtube#playlistItem',
            'snippet': {
                'playlistId': playlistId,
                'resourceId': {
                    'kind': 'youtube#video',
                    'videoId': videoId
                }
            }
        },
        error:function(jqXHR){
            console.log('moving item failed' + jqXHR.responseText);
        }
    });

编辑: 这是来自Chrome开发工具的请求标头,在我的localhost中工作:

:host:www.googleapis.com :method:OPTIONS :path:/youtube/v3/playlistItems :scheme:https :version:HTTP/1.1 accept:*/* accept-encoding:gzip,deflate,sdch accept-language:en-US,en;q=0.8,ms;q=0.6,zh-CN;q=0.4,zh;q=0.2,pt;q=0.2 access-control-request-headers:accept, contenttype, x-javascript-user-agent, authorization, content-type access-control-request-method:POST cache-control:no-cache origin:http://localhost pragma:no-cache referer:http://localhost/anything/blablala/blablabla/videos/add_videos_content.html user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36

这是相应的响应标题:

alternate-protocol:443:quic cache-control:no-cache, no-store, max-age=0, must-revalidate content-length:0 content-type:application/octet-stream date:Tue, 06 May 2014 01:50:27 GMT expires:Fri, 01 Jan 1990 00:00:00 GMT pragma:no-cache server:GSE status:200 OK version:HTTP/1.1 x-content-type-options:nosniff x-frame-options:SAMEORIGIN x-xss-protection:1; mode=block

编辑2:这是使用来自developers.google.com的测试API添加视频时的工作请求标头:

:host:content.googleapis.com :method:POST :path:/youtube/v3/playlistItems?part=snippet&key=myplaylistidthatIpurposelyhide :scheme:https :version:HTTP/1.1 accept:*/* accept-encoding:gzip,deflate,sdch accept-language:en-US,en;q=0.8,ms;q=0.6,zh-CN;q=0.4,zh;q=0.2,pt;q=0.2 authorization:Bearer myrandomaccesstokenthatIpurposelyhide cache-control:no-cache content-length:208 content-type:application/json origin:https://content.googleapis.com pragma:no-cache referer:https://content.googleapis.com/static/proxy.html?jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.PGgChEFan04.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Fz%3Dzcms%2Frs%3DAItRSTNppjT6YlqcSUdCS2FO0-uz5FYA6Q user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36 x-clientdetails:appVersion=5.0%20(Windows%20NT%206.1%3B%20WOW64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F34.0.1847.131%20Safari%2F537.36&platform=Win32&userAgent=Mozilla%2F5.0%20(Windows%20NT%206.1%3B%20WOW64)%20AppleWebKit%2F537.36%20(KHTML%2C%20like%20Gecko)%20Chrome%2F34.0.1847.131%20Safari%2F537.36 x-goog-encode-response-if-executable:base64 x-javascript-user-agent:google-api-javascript-client/1.0.0-alpha x-origin:https://developers.google.com x-referer:https://developers.google.com

这是相应的响应标题

alternate-protocol:443:quic cache-control:no-cache, no-store, max-age=0, must-revalidate content-encoding:gzip content-length:490 content-type:application/json; charset=UTF-8 date:Wed, 07 May 2014 00:53:35 GMT etag:"N5Eg36Gl054SUNiWWc-Su3t5O-k/DLbDf5tChxaDDEYTFBRlxLsiJl0" expires:Fri, 01 Jan 1990 00:00:00 GMT pragma:no-cache server:GSE status:200 OK version:HTTP/1.1 x-content-type-options:nosniff x-frame-options:SAMEORIGIN x-xss-protection:1; mode=block

0 个答案:

没有答案