c++ - 卡萨布兰卡中的 HTTP 身份验证

标签 c++ rest authentication casablanca

我在访问 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。

我已经建立了一个虚拟的幻想团队,这样我就可以安全地发布用户名和密码,以便人们可以试用。我不会将此密码用于其他任何用途。

#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,但输出表明找不到凭据。如果我在已经登录该站点时在 Chrome 中输入完整的 URL,我会得到我想要的 json 数据。如果我在未登录站点的 Edge 中输入 URL,我会得到与上面相同的输出(未提供凭据)。

我在这里是在鞭打一匹死马,还是有办法访问数据?

我不确定是否将此作为对原始答案的评论发布,但无论如何我没有足够的声誉来做到这一点。为重复道歉。

最佳答案

您需要使用授权访问您的数据。方法,您根本不必使用凭据配置,您只需要提供自定义 header 数据和客户端 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;   
}

不要忘记包含文件。

关于c++ - 卡萨布兰卡中的 HTTP 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47556223/

相关文章:

ruby-on-rails-3 - 如何使用 rspec 和 factory girl 设置我的身份验证数据?

c++迭代对象列表并删除对象

c++ - 将 std::cout 重定向到 QTextEdit

c++ - C++ 中基于 ifdef 的继承问题

node.js - Mongoose - REST API - 查询不同模型的模式

java - 为什么错误的路径注释不会使 Jersey 的 REST API 崩溃?

c++ - 在 C++ 程序中链接 cython 模块

java - JAX-RS - Wink - 使用 Wink 客户端读取文件的正确方法

javascript - 全局变量不会改变 Javascript 中的值

asp.net - 为什么 HttpContext.Current.User.Identity.Name 返回空白