簡體   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