How to publish Actions to Facebook Graph API with parameters in call URL

那年仲夏 提交于 2019-12-06 12:41:52

问题


To begin, Ive spent several hours looking for an answer and have come CLOSE but havent found a full answer.

So Im trying to publish actions using Facebook's Graph API and want to send parameters in the URL when initially calling it with the Facebook Javascript SDK.

Here is a call that WORKS:

  function postNewAction()
  {
          passString = '&object=http://mysite.com/appnamespace/object.php';

          FB.api('/me/APP_NAMESPACE:ACTION' + passString,'post',
             function(response) {
                  if (!response || response.error) {
                      alert(response.error.message);
                  } 
                  else {
                      alert('Post was successful! Action ID: ' + response.id);
                  } 
              }
          );    
   }

With object.php looking like so:

  <head xmlns:enemysearch="http://apps.facebook.com/APP_NAMESPACE/ns#">
    <meta property="fb:app_id" content="APP_ID" /> 
    <meta property="og:type" content="APP_NAMESPACE:object" /> 
    <meta property="og:title" content="some title" />
    <meta property="og:description" content="some description"/> 
    <meta property="og:locale" content="en_US" />
    <meta property="og:url" content="http://www.mysite.com/appnamespace/object.php"/>
  </head>

So obviously the above will post some sort of action to Facebook that will be the same every time object.php is called.

This is not ideal for my purposes.

So I took a look at this thread and it helped but as you will see, doesnt answer everything.

So if I change object.php to include dynamically generated meta tags by using content from either GET or POST and use the Object Debugger with a matching URL format to the one provided in og:url, the debugger shows everything as working as I would expect with now warnings. Here is what the dynamically generated object.php looks like:

   <?php
      if(count($_GET) > 0) {
     $userName = (int)$_GET['userName'];
      }
      else {
     $userName = (int)$_POST['userName'];
      }
   ?>
  <head xmlns:enemysearch="http://apps.facebook.com/APP_NAMESPACE/ns#">
    <meta property="fb:app_id" content="APP_ID" /> 
    <meta property="og:type" content="APP_NAMESPACE:object" /> 
    <meta property="og:title" content=" <?php echo $userName; ?>" />
    <meta property="og:description" content="Click to view more from <?php echo $userName; ?>"/> 
    <meta property="og:locale" content="en_US" />
    <meta property="og:url" content="http://mysite.com/appnamespace/object.php?userName=<?php echo $userName; ?>"/>
  </head>

SO, what I need to know is how to send url parameters in the initial call. If I use the debugger and enter something to the effect of http://mysite.com/appnamespace/object.php?userName=Brad everything works fine and the meta tags return content with that name inside of them. However if I try to pass the same url in the Javascript Call, it returns an error saying that my meta property og:type needs to be 'APP_NAMESPACE:object' (which as you see above, is what I have) instead of 'website' (which as you can see above, is something I am not using). Here is what that broken call would look like:

  function postNewAction(userName)
  {
      passString = '&object=http://mysite.com/appnamespace/object.php?userName=' + userName;

      FB.api('/me/APP_NAMESPACE:ACTION' + passString,'post',
         function(response) {
              if (!response || response.error) {
                  alert(response.error.message);
              } 
              else {
                  alert('Post was successful! Action ID: ' + response.id);
              } 
          }
      );    
  }

To be clear, the userName variable is only for example.

Any thoughts?


回答1:


I've not tested it but i see something different from the code examples in this link

http://developers.facebook.com/docs/opengraph/tutorial/#publish

In your code passString should start with ? as is the very beginning of the query string.

// so this :
passString = '&object=http://mysite.com/appnamespace/object.php?userName=' + userName;
// should be :
passString = '?object=http://mysite.com/appnamespace/object.php?userName=' + userName;

  FB.api('/me/APP_NAMESPACE:ACTION' + passString,'post',
     function(response) {
          if (!response || response.error) {
              alert(response.error.message);
          } 
          else {
              alert('Post was successful! Action ID: ' + response.id);
          } 
      }
  );    


来源:https://stackoverflow.com/questions/8982416/how-to-publish-actions-to-facebook-graph-api-with-parameters-in-call-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!