如何使用呼叫URL中的参数将动作发布到Facebook Graph API [英] How to publish Actions to Facebook Graph API with parameters in call URL

查看:113
本文介绍了如何使用呼叫URL中的参数将动作发布到Facebook Graph API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要开始,我花了几个小时寻找答案,并已经关闭,但没有找到完整的答案。

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

所以我试图使用Facebook的Graph API发布动作并希望在最初使用Facebook Javascript SDK调用URL时发送参数。

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.

这是一个电话,工作:

  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如下所示:

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>

所以显然以上将向Facebook发布某种行为,每次都是相同的对象。 php被调用。

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

这不适合我的目的。

所以我看了一下href =https://stackoverflow.com/questions/8431694/dynamic-generation-of-facebook-open-graph-meta-tags>这个线程,它帮助,但正如你将看到的,没有回答一切

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

所以如果我通过使用GET或POST中的内容将object.php更改为包含动态生成的元标记,并使用对象调试器,调试器会显示一切正常工作,正如我预期的那样。这是动态生成的object.php的样子:

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>

所以我需要知道的是如何在初始调用中发送url参数。如果我使用调试器并输入 http://mysite.com/ appnamespace / object.php?userName = Brad 一切正常,元标记返回包含该名称的内容。但是,如果我尝试在Javascript调用中传递相同的url,它会返回一个错误,指出我的元属性og:type需要是APP_NAMESPACE:object(如上所述,是我所拥有的)而不是网站 '(你可以从上面看到的是我没有使用的东西)。这是打破的呼叫如下所示:

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);
              } 
          }
      );    
  }

要清楚的是,userName变量只是例如

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

任何想法?

推荐答案

我没有测试过,但我看到不同的东西从这个链接的代码示例

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

在你的代码 passString 应以开头,就像查询字符串的开始一样。

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);
          } 
      }
  );    

这篇关于如何使用呼叫URL中的参数将动作发布到Facebook Graph API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆