Sign In Register

How can we help you today?

Start a new topic
Answered

RestAPI php LogEventRequest

 Hi, I try to use game RestAPI over php request. I do this like described here: https://docs.gamesparks.com/api-documentation/rest-apis/requests.html#!/Events/post_rs_credential_secret_LogEventRequest but I get "NOTAUTHORIZED" or "Request not authorized"(when I use DeviceAuthRequest). What am I doing wrong(tried with and without auth token)? Code below:


 

$url = 'https://KEY.preview.gamesparks.net/rs/SHORTCODE/SECRET/LogEventRequest';
$user = "xxx";
$pass = "xxx";
$userpass = base64_encode($user . ":" . $pass);
$authToken = "Basic " . $userpass;

$postData = array(
    '@class' => '.LogEventRequest',
    'eventKey' => 'EVENT_NAME'
);

$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => "Authorization: {$authToken}\r\n".
            "Content-Type: application/json\r\n",
        'content' => json_encode($postData)
    )
));

$response = file_get_contents($url, FALSE, $context);

 


Best Answer

Hi Marcin,


When testing the REST API in Preview, make sure that the credential you use is the "debug" credential. You can see this in the Credentials screen on the Portal.


For example, an example LogEventRequest in curl would look like this. 


curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
  "@class": ".LogEventRequest",
  "eventKey": "getBalance",
  "playerId": "57ecd772e441290485df4543"
}' 'https://E300018ZDdAx.preview.gamesparks.net/rs/debug/<secret>/LogEventRequest'


LogEventRequest's generally require the playerId field provided as Cloud Code runs with player context.


Let me know if this helps, and if you have any more questions please let me know.


-Pádraig


Answer

Hi Marcin,


When testing the REST API in Preview, make sure that the credential you use is the "debug" credential. You can see this in the Credentials screen on the Portal.


For example, an example LogEventRequest in curl would look like this. 


curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
  "@class": ".LogEventRequest",
  "eventKey": "getBalance",
  "playerId": "57ecd772e441290485df4543"
}' 'https://E300018ZDdAx.preview.gamesparks.net/rs/debug/<secret>/LogEventRequest'


LogEventRequest's generally require the playerId field provided as Cloud Code runs with player context.


Let me know if this helps, and if you have any more questions please let me know.


-Pádraig

Thank you it works:) Also I've fixed header

 

If anyone else is wondering, I thought I should share this considering this is the only useful thread I've found for php. If you're trying to make a php call to mongo or to the API in general, you can go to the documentation and refer to the curl statement that Pádraig posted above.



You can find the "Try this" links in the Rest API docs which reveals quite a bit more information on post calls. Some allow you to put in your Gamespark username, password, api key and state.


This one is a good example of this:


Click and it will then show your username as well as the appropriate url to make the call.


Looks like this:


Now navigate down to mongo and for this example, we'll go ahead and just use .find on a custom meta collection I've created. So, if you're following along, scroll down until you see this and then expand it.


There will be 3 fields that are required. Enter in a collection, put '{ }' in the dto input since we want to find all documents and then paste in your api key again. Hit the Try button below the input fields and you should get the results you're looking for, assuming that you have everything configured properly.


Example:

image



You may now copy and paste the curl statement into a curl-to-php converter to save a little time. Here's a good one that works pretty well - it will output php code which should work right out of box. Dissect it to see how it works.


https://incarnate.github.io/curl-to-php/

Login to post a comment