Is getplayer() failing?

Maniaplanet public API, ManiaConnect system and the open source PHP SDK.

Moderator: NADEO

Post Reply
MuNgLo
Posts: 314
Joined: 12 Jul 2012, 03:37

Is getplayer() failing?

Post by MuNgLo »

There must be some aspect I'm missing here. The problem is that the code below seems to fail setting up $player. I have the application as allowed on my player page and all. Infact the "login" button show up each time you restart ManiaPlanet it seems.

Code: Select all

<?php
// Change these values to match your MPWS user
define('MPWS_USER', 'NOWAY');
define('MPWS_PASS', 'NOWAYISAID');
define('MPWS_SCOPE', 'basic');

// Include the MPWS files
require('libraries/autoload.php');

try {
    // Try to retrieve the data of the player
    $mpwsPlayer = new \Maniaplanet\WebServices\ManiaConnect\Player(MPWS_USER, MPWS_PASS);
    $player = $mpwsPlayer->getPlayer();

    if (!$player) {
        // If $player equals false, we need the player to grant access to his or her data,
        // so we need to print the LoginURL
        $loginURL = htmlspecialchars($mpwsPlayer->getLoginURL(MPWS_SCOPE));
        $authLabel = true;
    } else {
        // If $player is not false, the player allowed accessing his or her data,
		$authLabel = false;
    }
} catch (\Maniaplanet\WebServices\Exception $e) {
    $message = htmlspecialchars("[{$e->getHTTPStatusCode()} {$e->getHTTPStatusMessage()}] {$e->getMessage()}", ENT_QUOTES, 'UTF-8'); 
    $error = '<label sizen="100 5" halign="center" text="Exception: '.$message.'" />';
}
?>
I've just started setting it up so I haven't really read it all but shouldn't the verification just pass as long as the player has accepted to let the application get access?

Oh and from the code above I use $authlabel to verify before letting users interact with anything. So far I've just setup a simple guestbook.

Also note that after you have pressed the login button you do get access and getplayer() passes until you restart ManiaPlanet again.
User avatar
m4rcel
Posts: 650
Joined: 15 Jun 2010, 11:12
Contact:

Re: Is getplayer() failing?

Post by m4rcel »

Each time you want to access the user's data, you have to authenticate him again. This means: When you want to get data from getPlayer(), you have to redirect him to the loginURL, e.g. using the <goto> Maniacode.

The difference on the second time is, that the user has not to confirm the access anymore. This means: You redirect him to the loginURL, ManiaConnect immediately redirects him back to your page, and you can access getPlayer().
ImageImage
Image
MuNgLo
Posts: 314
Joined: 12 Jul 2012, 03:37

Re: Is getplayer() failing?

Post by MuNgLo »

that actually makes sense :D

This is what I'm doing btw. Not much.... yet :D
bazinga
MuNgLo
Posts: 314
Joined: 12 Jul 2012, 03:37

Re: Is getplayer() failing?

Post by MuNgLo »

m4rcel wrote:Each time you want to access the user's data, you have to authenticate him again. This means: When you want to get data from getPlayer(), you have to redirect him to the loginURL, e.g. using the <goto> Maniacode.

The difference on the second time is, that the user has not to confirm the access anymore. This means: You redirect him to the loginURL, ManiaConnect immediately redirects him back to your page, and you can access getPlayer().
With a button to authenticate it works just fine. But I cant seem to get a redirect working. Can it be some encoding issues?
User avatar
magnetik
Posts: 1673
Joined: 01 Feb 2012, 19:13
Location: Bordeaux
Contact:

Re: Is getplayer() failing?

Post by magnetik »

It could be.

Could you give us the code you are using for the redirect and the exact url you are sending the user to ?
ManiaPlanet technical documentation portal (Dedicated, ManiaLink, ManiaScript, Titles...) -- contribute!
MuNgLo
Posts: 314
Joined: 12 Jul 2012, 03:37

Re: Is getplayer() failing?

Post by MuNgLo »

I've already made a mess of the code, spread over a bunch of files. :D
But even when I run tweaked versions of the example code below it won't work.

Examplecode taken from http://forum.funtrackers.net/showthread ... niaconnect

Code: Select all

<?php
// Change these values to match your MPWS user
define('MPWS_USER', XXXXXXXXX');
define('MPWS_PASS', 'XXXXXXXXX');
define('MPWS_SCOPE', 'basic');

// Include the MPWS files
require('libraries/autoload.php');

try {
    // Try to retrieve the data of the player
    $mpwsPlayer = new \Maniaplanet\WebServices\ManiaConnect\Player(MPWS_USER, MPWS_PASS);
    $player = $mpwsPlayer->getPlayer();

    if (!$player) {
        // If $player equals false, we need the player to grant access to his or her data,
        // so we need to print the LoginURL
        $loginURL = htmlspecialchars($mpwsPlayer->getLoginURL(MPWS_SCOPE));
        $label = '<label style="CardButtonMedium" text="Authentificate" manialink="'.$loginURL.'" />';
    } else {
        // If $player is not false, the player allowed accessing his or her data,
        // so we are able to e.g. print the Nickname
        $nickname = htmlspecialchars($player->nickname);
        $label = '<label sizen="100 5" halign="center" text="Your Nickname: '.$nickname.'" />';
    }
} catch (\Maniaplanet\WebServices\Exception $e) {
    // Accessing ManiaConnect or the WebServices in general has failed.
    // In this case, we simply print the error message.
    $message = htmlspecialchars("[{$e->getHTTPStatusCode()} {$e->getHTTPStatusMessage()}] {$e->getMessage()}", ENT_QUOTES, 'UTF-8'); 
    $label = '<label sizen="100 5" halign="center" text="Exception: '.$message.'" />';
}
// Output the ManiaLink
header('Content-Type: text/xml; charset=utf-8');
echo <<<EOT
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<manialink version="1">
<timeout>0</timeout>
{$label}
</manialink>
EOT;
?>
This works just fine and in terms of output xml without aouthentification it gives me

Code: Select all

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<manialink version="1">
<timeout>0</timeout>
<label style="CardButtonMedium" text="Authentificate" manialink="https://ws.maniaplanet.com/oauth2/authorize/?client_id=munglo%7Cbazinga&redirect_uri=http%3A%2F%2Fskynet-irc.com%2Fml%2Fbazinga.php&scope=basic&response_type=code" />
</manialink>
But then if I try to change the button for authentification to a redirect <goto> like say..

Code: Select all

 <?php
// Change these values to match your MPWS user
define('MPWS_USER', 'XXXXXXXXX');
define('MPWS_PASS', 'XXXXXXXXX');
define('MPWS_SCOPE', 'basic');

// Include the MPWS files
require('libraries/autoload.php');

try {
    // Try to retrieve the data of the player
    $mpwsPlayer = new \Maniaplanet\WebServices\ManiaConnect\Player(MPWS_USER, MPWS_PASS);
    $player = $mpwsPlayer->getPlayer();

    if (!$player) {
        // If $player equals false, we need the player to grant access to his or her data,
        // so we need to print the LoginURL
        $loginURL = htmlspecialchars($mpwsPlayer->getLoginURL(MPWS_SCOPE));
       //$label = '<label style="CardButtonMedium" text="Authentificate" manialink="'.$loginURL.'" />';
		$label = '<goto><link>'.$loginURL.'</link></goto>';
// Output the ManiaLink redirectLogin
header('Content-Type: text/xml; charset=utf-8');
echo <<<EOT
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<manialink version="1">
<timeout>0</timeout>
{$label}
</manialink>
EOT;
exit;
		}
	
$player = $mpwsPlayer->getPlayer();
if ($player) {
        // If $player is not false, the player allowed accessing his or her data,
        // so we are able to e.g. print the Nickname
        $nickname = htmlspecialchars($player->nickname);
        $label = '<label sizen="100 5" halign="center" text="Your Nickname: '.$nickname.'" />';
    }
} catch (\Maniaplanet\WebServices\Exception $e) {
    // Accessing ManiaConnect or the WebServices in general has failed.
    // In this case, we simply print the error message.
    $message = htmlspecialchars("[{$e->getHTTPStatusCode()} {$e->getHTTPStatusMessage()}] {$e->getMessage()}", ENT_QUOTES, 'UTF-8'); 
    $label = '<label sizen="100 5" halign="center" text="Exception: '.$message.'" />';
}
// Output the ManiaLink
header('Content-Type: text/xml; charset=utf-8');
echo <<<EOT
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<manialink version="1">
<timeout>0</timeout>
{$label}
</manialink>
EOT;
?>
Then it outputs in xml

Code: Select all

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<manialink version="1">
<timeout>0</timeout>
<goto><link>https://ws.maniaplanet.com/oauth2/authorize/?client_id=munglo%7Cbazinga&redirect_uri=http%3A%2F%2Fskynet-irc.com%2Fml%2Fbazinga.php&scope=basic&response_type=code</link></goto>
</manialink>
Which to me looks ok but still ManiaPlanet will not, get page->redirect->show nickname, as it did when using the button.
$player is still empty and in ManiaPlanet an empty page is shown.

Overall and as far as I can tell it is sticking to UTF-8 all the way.

Only thing that strikes me is the "redirect_uri" in the loginURL. :D Shouldn't it rather be redirect_url? But since I haven made that part and it worked with the button I guess it's ok as is.

I hope I'm just stupid as usual and someone more experienced can see where I have gone wrong. That's how it usually goes. :D
MuNgLo
Posts: 314
Joined: 12 Jul 2012, 03:37

Re: Is getplayer() failing?

Post by MuNgLo »

Hang on a minute. I've just found a big thing I was doing wrong.
The <goto> tag is maniacode and has to be inside <maniacode>.
The <maniacode> itself need to be the first thing in the xml.
Slap onto that a "noconfirmation='1' " and everything should make sense and work.

Sorry for the huge post above. Hope this can help someone as stupid as me.

EDIT
YeY :3 . got it working as intended in my maincode now to.

Code: Select all

<?php
// Change these values to match your MPWS user
define('MPWS_USER', 'XXXXXXXXXXXXX');
define('MPWS_PASS', 'XXXXXXXXXXXXX');
define('MPWS_SCOPE', 'basic');

// Include the MPWS files
require('libraries/autoload.php');

try {
    // Try to retrieve the data of the player
    $mpwsPlayer = new \Maniaplanet\WebServices\ManiaConnect\Player(MPWS_USER, MPWS_PASS);
    $player = $mpwsPlayer->getPlayer();

    if (!$player) {
        // If $player equals false, we need the player to grant access to his or her data,
        // so we need to print the LoginURL
        $loginURL = htmlspecialchars($mpwsPlayer->getLoginURL(MPWS_SCOPE));
        //$label = '<label style="CardButtonMedium" text="Authentificate" manialink="'.$loginURL.'" />';
		$label = '<goto><link>'.$loginURL.'</link></goto>';
// Output the ManiaLink redirectLogin
header('Content-Type: text/xml; charset=utf-8');
echo <<<EOT
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<maniacode noconfirmation="1">
{$label}
</maniacode>
<manialink version="1">
<timeout>0</timeout>
</manialink>
EOT;
exit;
		}
	
	$player = $mpwsPlayer->getPlayer();
	if ($player) {
        // If $player is not false, the player allowed accessing his or her data,
        // so we are able to e.g. print the Nickname
        $nickname = htmlspecialchars($player->nickname);
        $label = '<label sizen="100 5" halign="center" text="Your Nickname: '.$nickname.'" />';
    }
} catch (\Maniaplanet\WebServices\Exception $e) {
    // Accessing ManiaConnect or the WebServices in general has failed.
    // In this case, we simply print the error message.
    $message = htmlspecialchars("[{$e->getHTTPStatusCode()} {$e->getHTTPStatusMessage()}] {$e->getMessage()}", ENT_QUOTES, 'UTF-8'); 
    $label = '<label sizen="100 5" halign="center" text="Exception: '.$message.'" />';
}
// Output the ManiaLink
header('Content-Type: text/xml; charset=utf-8');
echo <<<EOT
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<manialink version="1">
<timeout>0</timeout>
{$label}
</manialink>
EOT;
?>
Post Reply

Return to “Maniaplanet Web Services”

Who is online

Users browsing this forum: No registered users and 1 guest