【发布时间】:2023-05-04 20:23:01
【问题描述】:
我在访问 Fantasy Premier League 数据时遇到问题,URL https://fantasy.premierleague.com/drf/my-team/1447063/
该 URL 是特定于我的,因此需要进行身份验证才能访问它。我在Set Basic HTTP Authentication in Casablanca 的答案中使用了代码更改凭据、它们的base64 转换、U 的URL(“https://fantasy.premierleague.com”)和L“/drf/my-team/1447063/”的URI。
我已经建立了一个虚拟幻想团队,以便我可以安全地发布用户 ID 和密码,以便人们可以尝试。我不会将此密码用于其他任何用途。
#include "stdafx.h"
#include <vector>
#include <cpprest\http_client.h>
int main()
{
// These lines are just to show the conversion to base64
char sCredentials[] = "robin@laddershakers.com:jrhf9YYal";
std::vector<unsigned char> vCred;
for (int x = 0; x < (sizeof(sCredentials) / sizeof(sCredentials[0])) - 1; x++)
vCred.push_back(sCredentials[x]); // There must be an easier way to do this
auto b64Credentials = utility::conversions::to_base64(vCred);
using namespace web::http::client;
using namespace web::http;
// Creating http_client
http_client_config config;
credentials cred(L"robin@laddershakers.com", L"jrhf9YYal");
config.set_credentials(cred);
http_client client(U("https://fantasy.premierleague.com/drf/my-team/1447063/"), config);
// create header
http_request req(methods::GET);
// Add base64 result to header
req.headers().add(L"Authorization", L"Basic cm9iaW5AbGFkZGVyc2hha2Vycy5jb206anJoZjlZWWFs");
// req.set_request_uri(L"/drf/my-team/1447063/");
pplx::task<http_response> responses = client.request(req);
pplx::task<web::json::value> jvalue = responses.get().extract_json();
std::wcout << jvalue.get().serialize();
return 0;
}
产生的输出是
{"detail":"未提供身份验证凭据。"}
http_response 的结果为 403 FORBIDDEN,这表明即使使用凭据也无法访问 URL,但输出表明找不到凭据。如果我在已经登录到该站点时将完整的 URL 输入到 Chrome 中,我会得到我想要的 json 数据。如果我将 URL 输入到尚未登录站点的 Edge 中,我会得到与上面相同的输出(未提供凭据)。
我在这里是在鞭打一匹死马,还是有办法访问数据?
我不确定是否将其作为对原始答案的评论发布,但无论如何我没有足够的声誉来做到这一点。为重复道歉。
【问题讨论】:
-
嗨伙计,你有没有得到这个工作?
-
嗨,我没有。我放弃了,改用屏幕抓取。
-
您好,这里的问题是它是服务器端问题。如果你使用url:fantasy.premierleague.com/drf/entry/team_id/event/24/picks你可以获得大部分相同的数据
-
好的,谢谢。我可以通过此链接获得的是我上周提交的团队的“公开”数据。这给了我大部分数据,但没有提供我需要的商品之一的售价。
标签: c++ rest authentication casablanca