Steam API 访问控制允许来源问题 [英] Steam API Access-Control-Allow-Origin Issue

查看:57
本文介绍了Steam API 访问控制允许来源问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对网络编程有点陌生,对此有点困惑.我有一个基本的 express node.js 网络服务器,用于提供一个网站.我想将一个游戏 ID 交给一个函数,并让它使用他们的 web api 从 Steam 获取成就信息,应该使用以下 REST api 调用来支持:

A bit new to web programming and a bit confused on this. I have a basic express node.js webserver serving up a web site. I want to hand a gameid to a function and have it grab achievement information from steam using their web api that should be supported using the following REST api call:

https://developer.valvesoftware.com/wiki/Steam_Web_API#GetGlobalAchievementPercentagesForApp_.28v0002.29

我正在提供一个脚本文件,我正在尝试在这样的客户端文件中执行请求.

I have a script file being served up I'm trying to do a request in a client side file like such.

function GetSteamGameAchievements(appid)
{
   var request = new XMLHttpRequest();

    request.addEventListener('load',function(){

       console.log(JSON.stringify(this.responseText));

    });

    request.open("GET","http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid="+ appid + "&format=json",true);

    request.send(null);
}

我收到以下错误

XMLHttpRequest 无法加载http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=440&format=json.请求中不存在Access-Control-Allow-Origin"标头资源.Origin 'http://localhost:3000' 因此是不允许的访问.

XMLHttpRequest cannot load http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid=440&format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

我已经阅读了一些有关该错误的信息,但似乎需要更改服务器端代码或其他内容.尽管所有服务器端代码都做了类似的事情:

I've read a bit about the error, but it seems to require server side code changes or something. Though all the server side code did something like:

// Add headers
app.use(function (req, res, next) {

  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', 'http://api.steampowered.com');

  // Request methods you wish to allow
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader('Access-Control-Allow-Credentials', true);

  // Pass to next layer of middleware
  next();
});

使用 Open Weather API,这是我使用过的唯一的其他 3rd 方 REST api,类似于原始代码,所以我不确定 Steam API 为何无法正常工作.使用 Postman,我提交的查询字符串很好,但是当我在客户端 javascript 中执行此操作时,证券有问题……但不是 Open Weather API ……我不确定为什么它适用于一个但不是另一个.任何帮助将不胜感激,我已经浏览了一些类似的主题,但我认为我的笨拙让我很难理解我哪里出错了,以及需要准确修复的地方.

Using Open Weather API which is the only other 3rd party REST api I've worked with something like the original code would be fine so I'm unsure why the Steam API isn't working right. Using Postman the query strings I'm submitting are fine, but there is something wrong with the securities when I do it in my client-side javascript ... but not Open Weather API ... i'm unsure why it works for one but not the other. Any help would be much appreciated, I've went over some similar threads but I think my noobishness is making it difficult for me to understand where I am going wrong and where the fix needs to be made exactly.

推荐答案

我已经阅读了一些有关该错误的信息,但似乎需要更改服务器端代码或其他内容.

I've read a bit about the error, but it seems to require server side code changes or something.

是 - 通过您要从中读取数据的服务.即通过 Steam.

Yes — by the service you want to read data from. i.e. by Steam.

您不能允许自己使用访问者的浏览器从其他人的网站读取数据.

You can't give yourself permission to read data from other people's sites using your visitors' browsers.

由于您无法更改 Steam 的服务器,您需要使用您的服务器而不是访问者的浏览器从 Steam 获取数据.

Since you can't alter Steam's servers, you'll need to fetch the data from Steam using your server instead of from the visitors' browsers.

这篇关于Steam API 访问控制允许来源问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆