Facebook Instant Articles:使用图表创建文章 API
Facebook Instant Articles: Creating Articles with Graph API
我一直在开发一个使用 Facebook API 来创建即阅文的 WordPress 插件。 (我使用的是此处的 SDK 版本 5.3.1:https://github.com/facebook/php-graph-sdk/tree/5.4)它需要包含许多自定义字段,因此需要使用新插件而不是使用现有插件。我使用 Vagrant/VirtualBox linux 服务器在我的本地计算机上开发了它,从那里我能够成功地进行身份验证并创建一个 Instant Article,然后它出现在 Facebook 的列表中,在 Publishing Tools > Instant Articles 中> 开发文章.
我将相同的代码部署到实时 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
}
使用实时服务器响应(失败):
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's suggestion in his comment I was able to discover the cause of the problem. After making a request to get the article import status,我在响应中发现了这个错误信息:
["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}"}"
然后我意识到,在构建 Instant Article 标记的函数中,我在标记上做了一个 str_replace
,这样我的本地测试域名将被替换为正确的 "claimed"域名。由于我们已将代码部署到新域,因此没有发生这种替换,并且 Facebook 在标记中发现了不正确的 "unclaimed" 域。
通过将新域名添加到要替换的名称数组中,我能够将标记发送到具有 "claimed" 域的即阅文。我的文章创建成功!
非常感谢 CBroe,我快疯了。
我一直在开发一个使用 Facebook API 来创建即阅文的 WordPress 插件。 (我使用的是此处的 SDK 版本 5.3.1:https://github.com/facebook/php-graph-sdk/tree/5.4)它需要包含许多自定义字段,因此需要使用新插件而不是使用现有插件。我使用 Vagrant/VirtualBox linux 服务器在我的本地计算机上开发了它,从那里我能够成功地进行身份验证并创建一个 Instant Article,然后它出现在 Facebook 的列表中,在 Publishing Tools > Instant Articles 中> 开发文章.
我将相同的代码部署到实时 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
}
使用实时服务器响应(失败):
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's suggestion in his comment I was able to discover the cause of the problem. After making a request to get the article import status,我在响应中发现了这个错误信息:
["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}"}"
然后我意识到,在构建 Instant Article 标记的函数中,我在标记上做了一个 str_replace
,这样我的本地测试域名将被替换为正确的 "claimed"域名。由于我们已将代码部署到新域,因此没有发生这种替换,并且 Facebook 在标记中发现了不正确的 "unclaimed" 域。
通过将新域名添加到要替换的名称数组中,我能够将标记发送到具有 "claimed" 域的即阅文。我的文章创建成功!
非常感谢 CBroe,我快疯了。