首页 > 解决方案 > LinkedIn Share API 'ugcPosts' 响应来自 PHP(Wordpress)的 504 网关超时

问题描述

我正在使用 wordpress 将我的帖子分享到linkedIn。为此,我使用https://api.linkedin.com/v2/ugcPosts API。但是这个 API 响应返回 504 网关超时。

在上一步中,当我调用另一个 API 来获取访问令牌时,它很容易获得了访问令牌。但是当我想使用 ucPosts POST api 请求创建共享时,它会提供网关超时。我在这里请求的代码。

请任何人帮助我。

尝试从 localhost apache 服务器(PHP,wordpress)

$params = '{
        "author" : "urn:li:person:'.$linkedInAppCredentials->get_user_URN().'",
        "lifecycleState" : "PUBLISHED",
        "specificContent" : {
            "com.linkedin.ugc.ShareContent" : {
                "shareCommentary" : {
                    "text" : "'.$message.'"
                },
                "shareMediaCategory" : "NONE"
            }},
        "visibility" :"PUBLIC"
}';
$headers = '{
    "Content-Type": "application/json",
    "X-Restli-Protocol-Version": "2.0.0",
    "x-li-format": "json",
    "Connection": "Keep-Alive",
    "Authorization": "Bearer '.$linkedInAppCredentials->getAccessToken().'"
}';

$requestedUrl = "https://api.linkedin.com/v2/ugcPosts?oauth2_access_token=".$this->getAccessToken();
$requestBody = array(
            'headers' => $header,
            'timeout' => 3600,
            'body' => $params
        );
 $result = wp_remote_post($requestedUrl, $requestBody);

响应:[body] => {"message":"Request timed out","status":504} [response] => Array ([code] => 504 [message] => Gateway Timeout)

标签: phpwordpresshttp-status-code-504

解决方案


请求超时可能是因为 LinkedIn 无法解析请求正文。因此,将 requestBody 转换为 JSON 字符串可能是个好主意。提供正确的 JSON 输入对我有用,我遇到了同样的问题。

正如另一个人所发生的那样: https ://stackoverflow.com/a/56786205/12578136


推荐阅读