【问题标题】:Posting Network update to LinkedIn in PHP using Oauth2 access_token使用 Oauth2 access_token 在 PHP 中将网络更新发布到 LinkedIn
【发布时间】:2013-03-21 15:17:14
【问题描述】:

LinkedIn REST API 的许多代码示例和工具都假设您已使用 Oauth1 进行识别,并因此获得了会员令牌和会员密码。

现在,LinkedIn 在此处提供的示例代码:http://developer.linkedin.com/documents/code-samples 可以获取 Oauth2 access_token。

获得access_token后,您可以使用代码示例中提供的fetch函数进行查询并获取会员的详细信息

function fetch($method, $resource, $body = '') {


 $params = array('oauth2_access_token' => $_SESSION['access_token'],
                    'format' => 'json',
              );

    // Need to use HTTPS
    $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
    // Tell streams to make a (GET, POST, PUT, or DELETE) request
    $context = stream_context_create(
                    array('http' =>
                        array('method' => $method,
                        )
                    )
                );


    // Hocus Pocus
    $response = file_get_contents($url, false, $context);

    // Native PHP object, please
    return json_decode($response);
}

好的,太好了,这非常有效。但是提供的 fetch 函数假定 body 为 NULL。 但是要使用/v1/people/~/person-activities 发布网络更新,您需要在正文中传递一些 XML。我尝试了很多方法,找到了几十个使用 Oauth 1.0 成员令牌和成员密码的示例。但是当您只有一个 Oauth2 access_token 时,我还没有找到任何可行的解决方案。所以问题是: 此 fetch 函数需要进行哪些更改才能在查询中传递 XML 正文?

【问题讨论】:

    标签: php oauth-2.0 linkedin


    【解决方案1】:

    终于,我找到了答案。

    function GetXMLTemplate(){
       string = '<?xml version="1.0" encoding="UTF-8"?><activity locale="en_US">
       <content-type>linkedin-html</content-type>
       <body>xml_content</body>
       </activity>';
       return $string;
     }
    
     public function PostActivity($message) {   
        $details = $this->GetLinkedInDetails(); //own function returning basic details in Stdobject
        $url = 'https://api.linkedin.com/v1/people/~/
           person-activities?oauth2_access_token='.$this->access_token;
        // build your message
        $txt = '<a href="'.$details->siteStandardProfileRequest->url.'">'.$details->firstName.
               ' '.$details->lastName.'</a> '.$message;
        //the below str_replace is only required because the activity contains a <a> tag
        //if you don't have one, just keep the htmlspecialchars
        $txt = str_replace("&","&amp;",htmlspecialchars($txt,ENT_QUOTES,'UTF-8'));
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS,str_replace('xml_content',$txt,$this->GetXMLTemplate()));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
        $response = curl_exec($ch);
        $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        print_r($response);
        echo $http_status;
    }
    

    【讨论】:

    • 如果您使用 json 而不是 XML,您不必处理 XML 和编码。我在linkedin 上做了一个稍微改进的例子,它提供了非常简单和无痛的集成,如果你喜欢,可以查看:github.com/EJTH/SLinkedIn/blob/master/examples/share.php
    • LinkedIn 建议 (developer.wordpress.com/docs/oauth2) 使用基于标头的授权,因为查询字符串可能会记录在不安全的地方。所以我建议从 URL 中删除 oauth2_access_token 并将 'Authorization: Bearer ' . $this-&gt;access_token 添加到 CURLOPT_HTTPHEADER 数组中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    相关资源
    最近更新 更多