繁体   English   中英

为什么在LinkedIn API上共享会返回400错误请求?

[英]Why Share on LinkedIn API return 400 Bad Request?

我正在尝试使用PHP库LinkedIn-Client API通过其在LinkedIn API 上的共享发布LinkedIn共享更新。 我已经成功授权了用户,并且获得了返回的访问令牌。 我将令牌与此共享API调用一起使用。 这是我的代码:

$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);

if ($accessToken) { // assume that it is retrieved from session
    $linkedInOAuth->setAccessToken($accessToken);

    $postParams = array(
        "content" => array(
            'description' => "I'm so exciting to share a post using API."
        ),
        "visibility" => array(
            "code" => "connnections-only"
        )
    );

    $result = $linkedInOAuth->api(
        "v1/people/~/shares",
        array("format" => "json"),
        "POST",
        $postParams
    );
}

但是,此API调用返回了400错误的请求错误。

致命错误:未被捕获的GuzzleException:400:客户端错误响应[url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxxxxx [状态码] 400 [原因短语]错误的请求被抛出... \\供应商\\ happyr \\ linkedin-api-client \\ src \\ Happyr \\ LinkedIn \\ Http \\ GuzzleRequest.php,第26行

可能是什么问题呢?

[世界标准时间2015-04-30 3:00 PM更新]

LinkedIn-Client API 内部将Guzzle用于HTTP请求。 我试图用GuzzleHttp上,无需使用Happyr\\LinkedIn\\LinkedIn->api()但同样的错误,并没有成功。

if ($accessToken) {
    $url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accessToken;

    $client = new GuzzleHttp\Client();

    $response = $client->post($url, array(
        'headers' => array(
            'Content-Type' => 'application/json',
            'x-li-format'  => 'json'
        ),
        'json' => array(
            'comment' => 'Check out developer.linkedin.com!',
            'content' => array(
                'description' => 'I\'m so exciting to share a post using API.'
            ),
            'visibility' => array(
                'code' => 'connections-only'
            )
        )
    ));
}

致命错误:带有消息“客户端错误响应[url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxxx [状态代码] 400 [状态代码]的未捕获异常'GuzzleHttp \\ Exception \\ ClientException'原因短语]“错误请求”,位于\\ vendor \\ guzzlehttp \\ guzzle \\ src \\ Exception \\ RequestException.php:89堆栈跟踪:#0 \\ vendor \\ guzzlehttp \\ guzzle \\ src \\ Subscriber \\ HttpError.php(33):GuzzleHttp \\ Exception \\ RequestException :: create(Object(GuzzleHttp \\ Message \\ Request),Object(GuzzleHttp \\ Message \\ Response))#1 \\ vendor \\ guzzlehttp \\ guzzle \\ src \\ Event \\ Emitter.php(109):GuzzleHttp \\ Subscriber \\ HttpError-> onComplete(Object(GuzzleHttp \\ Event \\ CompleteEvent),'complete')#2 \\ vendor \\ guzzlehttp \\ guzzle \\ src \\ RequestFsm.php(91):GuzzleHttp \\ Event \\ Emitter-> emit('complete',Object(Guz in第89行的\\ vendor \\ guzzlehttp \\ guzzle \\ src \\ Exception \\ RequestException.php

[2015年5月5日世界标准时间上午9:40更新]

在LinkedIn API页面上复制并使用了Share的 xml和json示例。 这次,错误更改为内部服务器错误。

if ($accessToken) {
    $format = 'xml';
    $url = 'https://api.linkedin.com/v1/people/~/shares?format='.$format.'&oauth2_access_token=' . $connect->accessToken;
    $postParams = array(
        "xml" => "
            <share>
                <comment>Check out developer.linkedin.com!</comment>
                <content>
                    <title>LinkedIn Developer Resources</title>
                    <description>Leverage LinkedIn's APIs to maximize engagement</description>
                    <submitted-url>https://developer.linkedin.com</submitted-url>
                    <submitted-image-url>https://example.com/logo.png</submitted-image-url>
                </content>
                <visibility>
                    <code>anyone</code>
                </visibility>
            </share>",
        "json" => array(
            "comment" => "Check out developer.linkedin.com!",
            "content" => array(
                "title" => "LinkedIn Developers Resources",
                "description" => "Leverage LinkedIn's APIs to maximize engagement",
                "submitted-url" => "https://developer.linkedin.com",
                "submitted-image-url" => "https://example.com/logo.png"
            ),
            "visibility" => array(
                "code" => "anyone"
            )
        )
    );

    $client = new GuzzleHttp\Client();

    if ($format === 'xml') {
        $response = $client->post($url, array(
            'body' => $postParams['xml']
        ));
    } else {
        $response = $client->post($url, array(
            'headers' => array(
                'Content-Type' => 'application/json',
                'x-li-format'  => 'json'
            ),
            'json' => $postParams['json']
        ));
    }
}

致命错误:消息为“服务器错误响应[url] https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=xxxx [状态代码] 500 [状态代码]的未捕获异常'GuzzleHttp \\ Exception \\ ServerException'原因短语]内部服务器错误”

根据Happyr \\ LinkedIn-API-client的Github问题,0.5.0中进行了一些更新,它解决了我的问题。 但是,LinkedIn尚未提供有关其Share API的文档。 必须注意以下信息:

  1. comment字段是共享更新内容。 名称comment导致混乱。
  2. 使用字段comment ,URL在共享更新内容中是可选的。
  3. content字段表示有关您共享的URL内容的快照。 为了更清楚地描述,它反映了开放图元标签;
    • content.title覆盖<meta property="og:title" content="..." />
    • content.description覆盖<meta property="description" content="..." />
    • content.title覆盖<meta property="og:title" content="..." />
    • content.submitted-url覆盖<meta property="og:url" content="..." />
    • content.submitted-image-url覆盖<meta property="og:image" content="..." />
  4. 使用content字段时,必填字段submitted-url 其余的是可选的。 如果丢失,则会返回400 Bad Request
  5. LinkedIn Share API示例代码中包含的URL会导致500 Internal Server Error 它们不应用于测试目的。

以下是使用Happyr \\ LinkedIn-API-client的API的正确用法。

(1)Happyr \\ LinkedIn-使用字段comment

$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);
// retrieve $accessToken from db or session
$linkedInOAuth->setAccessToken($accessToken);

$postParams = array(
    'json' => array(
        "comment" => "PHPLucidFrame - The simple, lightweight and flexible web application development framework http://phplucidframe.sithukyaw.com",
        "visibility" => array(
            "code" => "anyone"
        )
    )
);

$result = $linkedInOAuth->post('v1/people/~/shares', $postParams);

(2)Happyr \\ LinkedIn-使用字段content

$linkedInOAuth = new Happyr\LinkedIn\LinkedIn(LINKEDIN_APP_ID, LINKEDIN_APP_SECRET);
// retrieve $accessToken from db or session
$linkedInOAuth->setAccessToken($accessToken);

$postParams = array(
    'json' => array(
        "content" => array(
            "title" => "PHPLucidFrame",
            "description" => "The simple, lightweight and flexible web application development framework",
            "submitted-url" => "http://phplucidframe.sithukyaw.com"
        ),
        "visibility" => array(
            "code" => "anyone"
        )
    )
);

$result = $linkedInOAuth->post('v1/people/~/shares', $postParams);

以下是使用GuzzleHttp的API的正确用法。

(3)GuzzleHttp-使用字段comment

// retrieve $accessToken from db or session
$url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accessToken;
$client = new GuzzleHttp\Client();
$response = $client->post($url, array(
    "json" => array(
        "comment" => "PHPLucidFrame - The simple, lightweight and flexible web application development framework http://phplucidframe.sithukyaw.com",
        "visibility" => array(
            "code" => "anyone"
        )
    )
));

(4)GuzzleHttp-使用字段content

// retrieve $accessToken from db or session
$url = 'https://api.linkedin.com/v1/people/~/shares?format=json&oauth2_access_token=' . $accessToken;
$client = new GuzzleHttp\Client();
$response = $client->post($url, array(
    "json" => array(
        "content" => array(
            "title" => "PHPLucidFrame",
            "description" => "The simple, lightweight and flexible web application development framework",
            "submitted-url" => "http://phplucidframe.sithukyaw.com"
        ),
        "visibility" => array(
            "code" => "anyone"
        )
    )
));

字段commentcontent也可以一起使用。

这里只是一个快速的猜测,但是在将其发送到API之前,您可能需要将json_encode添加到$ postParams中。

$result = $linkedInOAuth->api(
    "v1/people/~/shares",
    array("format" => "json"),
    "POST",
    json_encode($postParams),
);

交替地

如果那行不通,我还会注意到Linkedin文档说以下内容。 您可以尝试添加这两个标头(如果尚未添加)。

默认情况下,所有API调用均期望以XML格式输入,但是如果您的应用更方便地以JSON格式提交数据,则可以通过包含以下两个HTTP标头来通知API他们将接收JSON格式的负载通话中的值:

内容类型:application / json

x-li格式:json

您的JSON帖子正文需要更正。 校验:

https://developer.linkedin.com/docs/share-on-linkedin

您需要遵循以下任一格式。

  1. 通过包含网址的评论共享

     { "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG", "visibility": { "code": "anyone" } } 
  2. 分享特定的价值

     { "comment": "Check out developer.linkedin.com!", "content": { "title": "LinkedIn Developers Resources", "description": "Leverage LinkedIn's APIs to maximize engagement", "submitted-url": "https://developer.linkedin.com", "submitted-image-url": "https://example.com/logo.png" }, "visibility": { "code": "anyone" } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM