2

I am trying to post a game achievement to Facebook using the PHP CURL code shown below.

I have two variables $non_sef_achievement and $sef_achievement.

Using $sef_achievement I get that I have an invalid type game and that game.achievements is required.

Using the $non_sef_achievement variable I get the following dumps when I register an achievement. Please note that I get true when I post a score and the other error is when I try to post the achievement.

Can you help me find what I am doing wrong?

[string] {"error":{"message":"Invalid token: \"103032446\". An ID has already been specified.","type":"OAuthException","code":2500}} = "Register: " Tooltip

[string] true = "Post Score: " Tooltip

[string] {"error":{"message":"Invalid token: \"1000234234602\". An ID has already been specified.","type":"OAuthException","code":2500}} = "Post Achievement: " Tooltip

Note Tokens have been edited.

    //Get the achievement
    $achievement_user = $this->achUser->event_user->username;
   // $achievement = 'http://apps.facebook.com/my_app/component/achievements/achievement/'.$this->achUser->ach->name.'-'.$this->achUser->ach->id;
     $non_sef_achievement = 'https://apps.facebook.com/my_app/index.php?option=com_jachievements&view=achievement&id='.$this->achUser->ach->id;
   // http://mysite.com/index.php?option=com_jachievements&view=achievement&id=1
     $sef_achievement = 'https://apps.facebook.com/my_app/achievements/achievement/'.$this->achUser->ach->id;

    //CHECK https 
    $achievement_display_order = 1;

    // Get an App Access Token
    $token_url = 'https://graph.facebook.com/oauth/access_token?'
        . 'client_id=' . $app_id
        . '&client_secret=' . $app_secret
        . '&grant_type=client_credentials';


    $token_response = file_get_contents($token_url);
    $params = null;
    parse_str($token_response, $params);
    $app_access_token = $params['access_token'];

    $achievement_registration_URL = 'https://graph.facebook.com/'.$app_id.'/achievements';

    $ch = curl_init($achievement_registration_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'achievement='.$non_sef_achievement.'&display_order='.$achievement_display_order.'&access_token='.$app_access_token);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);

 $ach_id = $this->achUser->ach->id;

    $db =& JFactory::getDBO();
    $tableName = $db->nameQuote('jos_ja_achievements_actions');
    $achparams = $db->nameQuote('params');
    $achtype = $db->nameQuote('community_addkarmapoints');
    $sql = "SELECT $achparams FROM ".$tableName." WHERE ach_id =  $ach_id AND `type_name` = 'community_addkarmapoints'" ; 
    $db->setQuery($sql);
    $row = $db->loadRow();
    $achparam = $row['0'];
    $str = explode('"',$achparam);
    $ach_points = $str['3'];

    $score = $ach_points;

         $score_URL = 'https://graph.facebook.com/' .$fbUserId. '/scores';      
    $ch = curl_init($score_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'score='.$score.'&access_token=' . $app_access_token);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);


         POST a user achievement         
    $achievement_URL = 'https://graph.facebook.com/'.$fbUserId.'/achievements';
    $ch = curl_init($achievement_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'achievement='.$non_sef_achievement.'&access_token=' . $app_access_token);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
Igy
  • 43,710
  • 8
  • 89
  • 115
ivias
  • 249
  • 1
  • 3
  • 15

1 Answers1

4

'An ID has already been specified' can be caused if you make a request like:

https://graph.facebook.com/[USER ID]/feed?id=[SOME OTHER ID]

In that example, the first '[USER ID]' in the path is Facebook's 'id' for the request, and specifying another ID as a parameter triggers the error.

In this case, the most likely reason is if your achievement URL has an '?id=' in the URL which isn't being escaped properly before you call the API - either check your encoding or change the name of the parameter you use in your achievement generation code

Igy
  • 43,710
  • 8
  • 89
  • 115
  • 1
    Actually, it does appear that you have an unencoded `?id=` in your achievement URL - i'm fairly sure this is the problem – Igy Jul 07 '12 at 09:03
  • Hi Igy. Encoding the URL helped me get over that error however I am now getting a new error When tryig to register an achievement its telling me that the apps.facebook.com/canvas_appname has an og type of game. When i use the facebook linter tool and put this url it does show as game and it requires game.achievement og:type. I dont see any settings where i can change this in the developers side. – ivias Jul 10 '12 at 00:36
  • Sounds like that should be a new question, but are you sure your app isn't doing some redirect, and that the URL itself is correct? – Igy Jul 10 '12 at 07:54
  • No, the app isnt doing any redirect. I dont understand why the facebook linter tools returns game when i put the url there. I put the apps.facebook.com/myappname and it tells me the type is game so thats why i get the error since it needs to be game.achievement – ivias Jul 10 '12 at 15:24