0

I'm trying to authenticate with xboxapi.com via curl in PHP and i keep getting unauthorized 401.

To connect to the API we need an Authentication header. This is sent as X-AUTH https://xboxapi.com/documentation

I'm using the following code for authentication:

$headerArr = array();
$headerArr[] = "X-AUTH: here i put my api key";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://xboxapi.com/v2/accountXuid');   
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

I can't figure out what's wrong with the code...

The second problem i'm having is when i'm trying to get data from the api (i'm logged in to check)

I'm using:

<script>
$(document).ready(function() {

 $.ajax({
        url: 'https://xboxapi.com/v2/accountXuid',
        dataType: 'jsonp',
        jsonpCallback: 'callback',
        jsonp: false,
    });
});
</script>

and in firebug console I'm getting:

SyntaxError: missing ; before statement.

OK i get it, it's JSON and not JSONP but in net response (in firebug) i see the JSON. Is there a way to load JSON and not JSONP? How can i see the results on screen?

Any other solution will be welcomed here! Thank you.

Sufian
  • 6,405
  • 16
  • 66
  • 120
  • did you ever get this working? Im trying to get it to work and gain access to the json I can see in firebug. – tamak Aug 14 '14 at 14:32

2 Answers2

0

This would be best served as two posts to SO; let's deal with the PHP side of things first. Set up a function that does just that, and eliminate the AJAX for the time being so we don't add more variables than we need to.

Next, I would remove as much as you can from that curl call. The following returns a JSON formatted string as I expect it would:

$url     = 'https://xboxapi.com/v2/accountXuid';
$headers = array(
    'X-AUTH: API_KEY'
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

print_r($result);

If/when you get that much working, post back and we can knock out the AJAX side.

Chords
  • 6,720
  • 2
  • 39
  • 61
0

I was having the same problem and came up with this solution. This example calls the profile endpoint from v2 of the xboxapi.com API.

First, we set up the curl variables. Remember to replace the #xuid# and #auth_key# variables I inserted with your own xuid and auth key (leave out the #'s).

$profileServiceUrl = "https://xboxapi.com/v2/#xuid#/profile";
$contentType = 'text/xml';          //probably not needed
$method = 'POST';                   //probably not needed
$auth = 'X-AUTH: #auth_key#';    //API Key

Next, we do our curling

$profileCurl = curl_init();
curl_setopt($profileCurl, CURLOPT_URL, $profileServiceUrl);
curl_setopt($profileCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($profileCurl, CURLINFO_HEADER_OUT, true);
curl_setopt($profileCurl, CURLOPT_HTTPHEADER, Array('Content-type: ' . $contentType . '; auth=' . $auth));
curl_setopt($profileCurl, CURLOPT_HTTPHEADER, Array($auth));

And finally we grab and decode the Json we receive from the API call.

$profileJson = curl_exec($profileCurl);
$profileData = json_decode($profileJson);

All together, it should look like this.

$profileServiceUrl = "https://xboxapi.com/v2/#xuid#/profile";
$contentType = 'text/xml';          //probably not needed
$method = 'POST';                   //probably not needed
$auth = 'X-AUTH: #auth_key#';    //API Key
$profileCurl = curl_init();
curl_setopt($profileCurl, CURLOPT_URL, $profileServiceUrl);
curl_setopt($profileCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($profileCurl, CURLINFO_HEADER_OUT, true);
curl_setopt($profileCurl, CURLOPT_HTTPHEADER, Array('Content-type: ' . $contentType . '; auth=' . $auth));
curl_setopt($profileCurl, CURLOPT_HTTPHEADER, Array($auth));
$profileJson = curl_exec($profileCurl);
$profileData = json_decode($profileJson);

If you are having issues grabbing the Json data, it should look something like this.

$gamerTag = $profileData->Gamertag;

I know this response is late, hope you see this and it helps!