Facebook即时文章:用图形API创建文章


Facebook Instant Articles: Creating Articles with Graph API

我一直在开发一个WordPress插件,使用Facebook的API来创建即时文章。(我使用SDK版本5.3.1从这里:https://github.com/facebook/php-graph-sdk/tree/5.4)它需要包含许多自定义字段,因此新的插件,而不是使用现有的一个。我使用Vagrant/VirtualBox linux服务器在我的本地机器上开发它,从那里我能够成功验证并创建即时文章,然后出现在Facebook的发布工具>即时文章>开发文章列表中。

我将相同的代码部署到一个实时web服务器上以继续测试它。身份验证仍然成功,但当我试图从该服务器发布一篇文章时,即时文章永远不会创建,也不会出现在Facebook上的列表中。

令人费解的是,两次尝试的反应似乎几乎相同。一个区别是accessToken值,我将其标记为{access_token}。其他不同的回答我标记为{different}

谁能帮助我理解为什么一个成功,另一个失败没有任何明显的错误在PHP或在API响应?

使用本地机器的响应(成功):

object(Facebook'FacebookResponse)#144 (6) {
  ["httpStatusCode":protected]=>
  int(200)
  ["headers":protected]=>
  array(13) {
    ["Access-Control-Allow-Origin"]=>
    string(1) "*"
    ["Pragma"]=>
    string(8) "no-cache"
    ["Cache-Control"]=>
    string(44) "private, no-cache, no-store, must-revalidate"
    ["facebook-api-version"]=>
    string(4) "v2.7"
    ["Expires"]=>
    string(29) "Sat, 01 Jan 2000 00:00:00 GMT"
    ["Content-Type"]=>
    string(31) "application/json; charset=UTF-8"
    ["x-fb-trace-id"]=>
    string(11) "{different}"
    ["x-fb-rev"]=>
    string(7) "{different}"
    ["Vary"]=>
    string(15) "Accept-Encoding"
    ["X-FB-Debug"]=>
    string(88) "{different}"
    ["Date"]=>
    string(29) "Wed, 19 Oct 2016 16:24:51 GMT"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Content-Length"]=>
    string(2) "25"
  }
  ["body":protected]=>
  string(25) "{"id":"{different}"}"
  ["decodedBody":protected]=>
  array(1) {
    ["id"]=>
    string(16) "{different}"
  }
  ["request":protected]=>
  object(Facebook'FacebookRequest)#145 (9) {
    ["app":protected]=>
    object(Facebook'FacebookApp)#183 (2) {
      ["id":protected]=>
      string(16) "{app_id}"
      ["secret":protected]=>
      string(32) "{app_secret}"
    }
    ["accessToken":protected]=>
    string(168) "{access_token}"
    ["method":protected]=>
    string(4) "POST"
    ["endpoint":protected]=>
    string(29) "/{some_endpoint_id}/instant_articles"
    ["headers":protected]=>
    array(1) {
      ["Content-Type"]=>
      string(33) "application/x-www-form-urlencoded"
    }
    ["params":protected]=>
    array(3) {
      ["development_mode"]=>
      string(1) "1"
      ["published"]=>
      bool(false)
      ["html_source"]=>
      string(1600) "<html> ... </html>"
    }
    ["files":protected]=>
    array(0) {
    }
    ["eTag":protected]=>
    NULL
    ["graphVersion":protected]=>
    string(4) "v2.7"
  }
  ["thrownException":protected]=>
  NULL
}

使用Live Server的响应(failed):

object(Facebook'FacebookResponse)#107 (6) {
  ["httpStatusCode":protected]=>
  int(200)
  ["headers":protected]=>
  array(13) {
    ["Access-Control-Allow-Origin"]=>
    string(1) "*"
    ["Pragma"]=>
    string(8) "no-cache"
    ["Cache-Control"]=>
    string(44) "private, no-cache, no-store, must-revalidate"
    ["facebook-api-version"]=>
    string(4) "v2.7"
    ["Expires"]=>
    string(29) "Sat, 01 Jan 2000 00:00:00 GMT"
    ["Content-Type"]=>
    string(31) "application/json; charset=UTF-8"
    ["x-fb-trace-id"]=>
    string(11) "{different}"
    ["x-fb-rev"]=>
    string(7) "{different}"
    ["Vary"]=>
    string(15) "Accept-Encoding"
    ["X-FB-Debug"]=>
    string(88) "{different}"
    ["Date"]=>
    string(29) "Wed, 19 Oct 2016 16:02:08 GMT"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Content-Length"]=>
    string(2) "24"
  }
  ["body":protected]=>
  string(24) "{"id":"{different}"}"
  ["decodedBody":protected]=>
  array(1) {
    ["id"]=>
    string(15) "{different}"
  }
  ["request":protected]=>
  object(Facebook'FacebookRequest)#106 (9) {
    ["app":protected]=>
    object(Facebook'FacebookApp)#126 (2) {
      ["id":protected]=>
      string(16) "{app_id}"
      ["secret":protected]=>
      string(32) "{app_secret}"
    }
    ["accessToken":protected]=>
    string(164) "{access_token}"
    ["method":protected]=>
    string(4) "POST"
    ["endpoint":protected]=>
    string(29) "/{some_endpoint_id}/instant_articles"
    ["headers":protected]=>
    array(1) {
      ["Content-Type"]=>
      string(33) "application/x-www-form-urlencoded"
    }
    ["params":protected]=>
    array(3) {
      ["development_mode"]=>
      string(1) "1"
      ["published"]=>
      bool(false)
      ["html_source"]=>
      string(1639) "<html> ... </html>"
    }
    ["files":protected]=>
    array(0) {
    }
    ["eTag":protected]=>
    NULL
    ["graphVersion":protected]=>
    string(4) "v2.7"
  }
  ["thrownException":protected]=>
  NULL
}

感谢CBroe在他的评论中的建议,我能够发现问题的原因。在请求获取文章导入状态后,我在响应中发现了以下错误消息:

["body":protected]=>
  string(439) "{"errors":[{"level":"ERROR","message":"Unclaimed URL: The URL http:'/'/www.example.com'/my-article-url'/ has not been claimed for Instant Articles. Please check to make sure you have a URL registered for your page. For more information refer to URLs in the Publishing Articles section of the Instant Articles documentation."}],"status":"FAILED","id":"{some_id}"}"

然后我意识到,在构建即时文章标记的函数中,我正在对标记进行str_replace操作,以便将我的本地测试域名替换为正确的"声明"域名。由于我们已经将代码部署到一个新的域,这种替换没有发生,Facebook在标记中看到了一个错误的"未声明的"域。

通过将新域名添加到要替换的名称数组中,我能够将标记发送到具有"已声明"域的Instant Articles。我的文章创建成功了!

谢谢你,我都快疯了