在卡萨布兰卡设置基本HTTP身份验证 [英] Set Basic HTTP Authentication in Casablanca

查看:161
本文介绍了在卡萨布兰卡设置基本HTTP身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改 Casablanca教程以包括基本的HTTP身份验证来访问 Prosper API

I'm trying to modify the Casablanca tutorial to include basic HTTP authentication to access the Prosper API:

auto fileStream = std::make_shared<ostream>();

// Open stream to output file.
auto requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
    *fileStream = outFile;

    // Create http_client to send the request.
    http_client_config config;
    credentials creds( "username", "password" );
    config.set_credentials( creds );
    http_client client( U( "https://api.prosper.com/" ), config );

    // Build request URI and start the request.
    uri_builder builder(U("/api/Listings/"));

    return client.request( methods::GET, builder.to_string() );
})

...

不幸的是,我不断收到错误401-未经授权。但是,我可以通过 https:// username:password@api.prosper.com/api/Listings/ 访问浏览器中的页面,并且可以使用Casablanca来获取到不需要身份验证的常规网页。

Unfortunately, I keep getting error 401 - unauthorized. However, I can access the page in the browser via https://username:password@api.prosper.com/api/Listings/, and I can use Casablanca to get to regular web pages that don't need authentication.

我一般都是REST和Web知识的新手,文档毫无用处- http_client_config 用于设置可能的配置选项。别开玩笑了我什至不确定我是否使用了正确的类-这些东西看起来似乎正确。

I'm new to REST and web stuff in general, and the documentation is useless - http_client_config is "used to set the possible configuration options." No kidding. I'm not even sure if I'm using the right classes - these things just kind of looked right.

如何向http_client添加基本身份验证在卡萨布兰卡请求吗?

推荐答案

您需要在包含的请求中添加标头您的用户名:密码中的base64 例如

// Please check how to convert into base64

XYZtaW46Wr6yZW0xMAXY = base64("username:password")

// Creating http_client
http_client_config config;
credentials cred(L"username", L"Password");
config.set_credentials(cred);
http_client client(U("https://serverip"),config);
// create header
http_request req(methods::GET);
// Add base64 result to header
req.headers().add(L"Authorization", L"Basic XYZtaW46Wr6yZW0xMAXY");
req.set_request_uri(L"/api/json/xyz");
pplx::task<http_response> responses = client.request(req);
pplx::task<web::json::value> jvalue = responses.get().extract_json();
wcout << jvalue.get().to_string();

这篇关于在卡萨布兰卡设置基本HTTP身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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