简体   繁体   English

Access-Control-Allow-Origin不允许使用的URL

[英]URL not allowed by Access-Control-Allow-Origin

I am trying to implement OAUTH for accessing Flickr APIs. 我正在尝试实现OAUTH以访问Flickr API。 My AJAX call to flickr.com keeps failing. 我对flickr.com的AJAX调用一直没能。

Sample Error Message: 示例错误消息:

XMLHttpRequest cannot load http://www.flickr.com/services/oauth        /request_token?oauth_callback=oob&oauth…signature_method=HMAC-SHA1&oauth_timestamp=1368375405647&oauth_version=1.0. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin. 

Initially I used chrome and read the html file as file://path. 最初我使用chrome并将html文件作为file:// path读取。 I used to get the error 'null not allowed by access-control-allow-origin'. 我曾经得到错误'null-access-control-allow-origin不允许'。 I solved this problem by copying the html file to 'local IIS server', 'local python webserver' and then a 'remote webserver'. 我通过将html文件复制到“本地IIS服务器”,“本地python web服务器”,然后是“远程网络服务器”来解决了这个问题。 I created python web server using > python -m http.server 8080' 我使用> python -m http.server 8080'创建了python web服务器

I realize my cross browser call to flickr.com using XMLHttpRequest is failing. 我意识到我使用XMLHttpRequest对flickr.com的跨浏览器调用失败了。 I tried by various solutions suggested in this forum: 我试过这个论坛中建议的各种解决方案:

  1. Using newer Chrome 26.0.1410.64 m, which I guess supports CORS 使用较新的Chrome 26.0.1410.64 m,我猜这支持CORS
  2. I launched chrome with --disable-web-security 我用--disable-web-security启动了chrome
  3. I created a web server using python -m http.server 8080 on local machine and then on a remote machine and copied the html file to the site 我在本地计算机上使用python -m http.server 8080创建了一个Web服务器,然后在远程计算机上将html文件复制到站点
  4. I copied file to a local MSFT IIS server 我将文件复制到本地MSFT IIS服务器
  5. I defined URL in etc/hosts file to avoid numeric IP 我在etc / hosts文件中定义了URL以避免数字IP

I still get the same error (with relevant URL in the error message) 我仍然得到相同的错误(错误消息中的相关URL)

code clipping: 代码剪辑:

urlString="http://www.flickr.com/services/oauth/request_token?"+
        "oauth_callback="+"oob"+'&'+
        "oauth_consumer_key="+consumerKey+'&'+
        "oauth_nonce="+nonce+'&'+
        "oauth_signature="+esignature+'&'+
    "oauth_signature_method="+macAlgorithm+'&'+
        "oauth_timestamp="+timeStamp+'&'+
        "oauth_version=1.0";


$.ajax({
   url: urlString,
   success:function(data){
    alert(data);
  }
});

In order to CORS work, both ends must enable it. 为了CORS工作, 两端都必须启用它。

The first end is the browser , and, as you are using Chrome 26.*, yours is ok. 第一个结束是浏览器 ,并且,当您使用Chrome 26. *时,您的浏览器就可以了。

The second end is the server : 第二端是服务器

Before making a GET request to a domain different than the one the page is on, the browser sends an OPTIONS request to that domain. 在向不同于该页面的域的域发出GET请求之前,浏览器向该域发送OPTIONS请求。 In response to this request, the server should include some headers that tell if a cross-domain request ( GET , POST or other) is allowed. 为响应此请求,服务器应包含一些标头 ,用于指示是否允许跨域请求( GETPOST或其他)。

One of those headers is Access-Control-Allow-Origin . 其中一个标题是Access-Control-Allow-Origin

So when you run your page from your file system ( file:// "protocol"), the OPTIONS means something like "Flickr, can I make a cross-domain call to you? I'm calling from null " . 因此,当您从文件系统( file:// “协议”)运行页面时, OPTIONS意味着类似“Flickr,我可以对您进行跨域调用吗?我从null调用” Flickr does not recognize that domain as allowed and returns the error you are getting. Flickr无法将该域识别为允许并返回您获得的错误。

Same way, when you run your page from your local server, the OPTIONS says "(...) I'm calling from localhost:8080 " . 同样,当您从本地服务器运行页面时, OPTIONS“(...)我正在从localhost:8080调用” Flickr does not recognize that domain as allowed as well. Flickr也不会将该域识别为允许。

The solution: 解决方案:

I don't know the Flickr oauth service, but I know that, as any other service, to make a CORS call to it, the page must be in a domain allowed by it. 我不知道Flickr oauth服务,但我知道,与任何其他服务一样,要对其进行CORS调用,该页面必须位于其允许的域中。 From your tests, I'm guessing Flickr does't allow many other domains. 从你的测试来看,我猜测Flickr不允许其他许多域名。

But... an alternative to CORS is JSONP. 但是...... CORS的另一种选择是JSONP。 I did a little research, Flickr oauth seems to support it. 我做了一点研究,Flickr oauth似乎支持它。

Check this page for details: http://www.flickr.com/services/api/explore/flickr.auth.oauth.getAccessToken There's another question talking about that specific subject: Is JSONP supported in the new Flickr OAuth API? 查看此页面了解详细信息: http//www.flickr.com/services/api/explore/flickr.auth.oauth.getAccessToken还有另一个问题在讨论该特定主题: 新的Flickr OAuth API是否支持JSONP?

About JSONP, this can get you started: How to make a JSONP request from Javascript without JQuery? 关于JSONP,这可以帮助您入门: 如何在没有JQuery的情况下从Javascript发出JSONP请求?

It is not possible to implement Oauth 1.0 through just javascript without any server side script. 没有任何服务器端脚本,只能通过javascript实现Oauth 1.0。 Since the flickr's new authentication process is based on Oauth 1.0a. 由于flickr的新身份验证过程基于Oauth 1.0a。 You got to use a server-side script. 你必须使用服务器端脚本。

I tried to send the token request using JSONP in FireFox with CORS on(using a third-party add-on) and it worked fine. 我尝试使用带有CORS的FireFox中的JSONP发送令牌请求(使用第三方附加组件)并且它工作正常。 But without using any add-ons, it's not possible as the response from flickr is in text format(not in a JSON format) and the request fails. 但是没有使用任何附加组件,因为flickr的响应是文本格式(不是JSON格式)并且请求失败,所以不可能。

You can either use server-side code for token request. 您可以使用服务器端代码进行令牌请求。 OR Use the deprecated flickr API for authentication. 或使用已弃用的flickr API进行身份验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Access-Control-Allow-Origin不允许使用Origin url - Origin url is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin 不允许 Origin - Origin is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin不允许起源 - Origin is not allowed by Access-Control-Allow-Origin Access-Control-Allow-Origin 不允许 XMLHttpRequest - XMLHttpRequest is not allowed by Access-Control-Allow-Origin 起源<origin>不允许访问控制允许来源</origin> - Origin <origin> is not allowed by Access-Control-Allow-Origin 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问原始“呼叫URL” - No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘calling URL’ is therefore not allowed access Android webview - XMLHttpRequest无法加载Origin <url> Access-Control-Allow-Origin不允许使用 - Android webview - XMLHttpRequest cannot load Origin <url> is not allowed by Access-Control-Allow-Origin XMLHttpRequest无法加载url Access-Control-Allow-Origin不允许使用Origin null - XMLHttpRequest cannot load the url Origin null is not allowed by Access-Control-Allow-Origin 在本地服务器的2个端口上运行2个应用程序时,Access-Control-Allow-Origin不允许使用来源[URL] - Origin [URL] is not allowed by Access-Control-Allow-Origin when running 2 applications on 2 ports on my local server XmlHttpRequest无法加载ajax调用URL。 Access-Control-Allow-Origin不允许使用原始域 - XmlHttpRequest cannot load ajax call url. Origin domain is not allowed by Access-Control-Allow-Origin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM