【问题标题】:HTTP Authentication in Casablanca卡萨布兰卡的 HTTP 身份验证
【发布时间】: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


【解决方案1】:

您需要使用 Auth 访问您的数据。方法,您根本不必使用凭证配置,您只需要提供自定义标头数据和客户端 URL。

它的工作原理如下:

pplx::task<void> HTTPRequestCustomHeadersAsync(){
http_client client(L"https://fantasy.premierleague.com/drf/my-team/1463628/");
// Manually build up an HTTP request with header and request URI.
http_request request(methods::GET);
request.headers().add(L"Authorization", L"Basic am9obi5kb2VAZ21haWwuY29tOmFiYzEyMw==");

pplx::task<http_response> responses = client.request(request);
pplx::task<web::json::value> jvalue = responses.get().extract_json();
std::wcout << jvalue.get().serialize();

return client.request(request).then([](http_response response)
{
    // Print the status code.
    std::wostringstream ss;
    ss << L"Server returned returned status code " << response.status_code() << L"." 
<< std::endl;
    std:: wcout << ss.str();

}

);
}

int main(int argc, char *args[])

{
std::wcout << L"Calling HTTPRequestCustomHeadersAsync..." << std::endl;
HTTPRequestCustomHeadersAsync().wait();

return 0;   
}

不要忘记包含文件。

【讨论】:

  • 我已经尝试过这段代码,但仍然收到 403 错误。输出如下:
  • 正在调用 HTTPRequestCustomHeadersAsync... {"detail":"未提供身份验证凭据。"}
  • 我已经编辑了原始问题以使用我专门为这个问题设置的新幻想团队,以便如果您想尝试,我可以发布用户名和密码。