使用 PECL OAuth 通过 PHP 的 REST API 发出 LinkedIn 邀请请求的错误

发布于 2024-11-13 07:16:33 字数 2220 浏览 1 评论 0原文

我将 PECL OAuth 与 LinkedIn 一起使用,因为它可以很好地处理 Twitter 和 LinkedIn 的 GET 请求。如果这不起作用,将切换到 LinkedIn 自己的 OAuth 库,但更愿意通过相同的 OAuth 库完成所有操作。

代码:

$xmlString = "<?xml version='1.0' encoding='UTF-8'?>".
                         "<mailbox-item>".
                          "<recipients>".
                            "<recipient>".
                             "<person path=\"/people/email={$person->PersonEmail}\">".
                              "<first-name>{$person->PersonFirstName}</first-name>".
                              "<last-name>{$person->PersonLastName}</last-name>".
                             "</person>".
                            "</recipient>".
                          "</recipients>".
                          "<subject>Invitation to Connect</subject>".
                          "<body>Please join my professional network on LinkedIn.</body>".
                          "<item-content>".
                           "<invitation-request>".
                            "<connect-type>friend</connect-type>".
                           "</invitation-request>".
                          "</item-content>".
                         "</mailbox-item>";

try {
                $oauth = new OAuth(self::$apiKey, self::$secretKey, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);
                $oauth->setToken($accessToken, $accessTokenSecret);
                $oauth->fetch("http://api.linkedin.com/v1/people/~/mailbox", $xmlString, OAUTH_HTTP_METHOD_POST, array('Content-Type'=>'text/xml'));
                $result = $oauth->getLastResponse();
                return $result;
            } catch (OAuthException $e) {
                var_dump($e);
                throw new ModelException('Error - Invalid Access Token', ModelException::$excodes['invalid_access_token']);
            }

这会引发一个被捕获的异常,并且 var_dump 会吐出很多内容,主要是:“无效的身份验证/错误请求(收到 401,预期 HTTP/1.1 20X 或重定向)”

我尝试了多种变体代码。例如,删除 'Content-Type'=>'text/xml' 会产生相同的错误。我已经验证了 xml,并且我知道访问令牌很好,因为我可以很好地使用它们来发出 GET 请求。

任何帮助将不胜感激,谢谢。

I'm using PECL OAuth with LinkedIn because it is working fine with Twitter and LinkedIn's GET requests. Will switch to LinkedIn's own OAuth libraries if this fails to work, but would prefer to do everything through the same OAuth library.

Code:

$xmlString = "<?xml version='1.0' encoding='UTF-8'?>".
                         "<mailbox-item>".
                          "<recipients>".
                            "<recipient>".
                             "<person path=\"/people/email={$person->PersonEmail}\">".
                              "<first-name>{$person->PersonFirstName}</first-name>".
                              "<last-name>{$person->PersonLastName}</last-name>".
                             "</person>".
                            "</recipient>".
                          "</recipients>".
                          "<subject>Invitation to Connect</subject>".
                          "<body>Please join my professional network on LinkedIn.</body>".
                          "<item-content>".
                           "<invitation-request>".
                            "<connect-type>friend</connect-type>".
                           "</invitation-request>".
                          "</item-content>".
                         "</mailbox-item>";

try {
                $oauth = new OAuth(self::$apiKey, self::$secretKey, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);
                $oauth->setToken($accessToken, $accessTokenSecret);
                $oauth->fetch("http://api.linkedin.com/v1/people/~/mailbox", $xmlString, OAUTH_HTTP_METHOD_POST, array('Content-Type'=>'text/xml'));
                $result = $oauth->getLastResponse();
                return $result;
            } catch (OAuthException $e) {
                var_dump($e);
                throw new ModelException('Error - Invalid Access Token', ModelException::$excodes['invalid_access_token']);
            }

This throws an exception which is caught, and the var_dump spits out a lot, primarily: "Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)"

I've tried a number of variations with the code. For example dropping the 'Content-Type'=>'text/xml' and that gives the same error. I've validated the xml and I know the access tokens are good, as I'm using them just fine to make GET requests.

Any help would be appreciated, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的影子我的梦 2024-11-20 07:16:33

找到了解决方案,将其替换

$oauth = new OAuth(self::$apiKey, self::$secretKey, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);

$oauth = new OAuth(self::$apiKey, self::$secretKey, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);

Found the solution, it was replacing

$oauth = new OAuth(self::$apiKey, self::$secretKey, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_FORM);

with

$oauth = new OAuth(self::$apiKey, self::$secretKey, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_AUTHORIZATION);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文