1

I have a problem accessing Fantasy Premier League data, URL https://fantasy.premierleague.com/drf/my-team/1447063/

The URL is specific to me, so requires authentication to access it. I have used the code in the answer to Set Basic HTTP Authentication in Casablanca changing the credentials, the base64 conversion of them, the URL to U("https://fantasy.premierleague.com") and the URI to L"/drf/my-team/1447063/".

I have set up a dummy fantasy team so that I can safely publish the userid and password so that people can try it out. I don't use this password for anything else.

#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;
}

The output produced is

{"detail":"Authentication credentials were not provided."}

The http_response has result 403 FORBIDDEN, which suggests the URL is not accessible even with credentials, but the output suggests that the credentials are not found. If I enter the full URL into Chrome when I am already logged in to the site, I get the json data I want. If I enter the URL into Edge where I have not logged on to the site, I get the same output as above (credentials not provided).

Am I flogging a dead horse here, or is there a way to access the data?

I wasn't sure whether to post this as a comment to the original answer, but anyway I don't have enough reputation to do that. Apologies for the duplication.

Pboris
  • 11
  • 6
  • Hi man, did you ever get this working? – PKGrem Jan 24 '18 at 00:54
  • Hi, no I didn't. I gave up and used a screen-scrape instead. – Pboris Jan 25 '18 at 11:32
  • Hi man, the issue here is that it is a server side issue. If you use the url: https://fantasy.premierleague.com/drf/entry/team_id/event/24/picks You can get most of the same data – PKGrem Jan 25 '18 at 12:15
  • OK, thanks. What I can get with this link is 'public' data on the team I submitted the previous week. That gives me most of the data, but not selling price which is one of the items I need. – Pboris Jan 27 '18 at 12:27

1 Answers1

0

You need to access your data using the Auth. method, you don't have to use the credential configuration at all, all you need is providing the custom header data and the client URL.

This works as the following:

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;   
}

Don't forget the include files.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • I've tried this code, but still getting the 403 error. Output is as follows: – Pboris Jul 28 '18 at 12:08
  • Calling HTTPRequestCustomHeadersAsync... {"detail":"Authentication credentials were not provided."} – Pboris Jul 28 '18 at 12:09
  • I've edited the original question to use a new fantasy team I've set up just for this question so that I can publish username and password if you want to try it out. – Pboris Jul 28 '18 at 12:36