LinkedIn 未阅读 JSON 请求

LinkedIn not reading JSON request

尝试在 LinkedIn 上 'share':https://developer.linkedin.com/docs/share-on-linkedin

这基本上是 POST 到 'https://api.linkedin.com/v1/people/~/shares?format=json' 最简单的形式是 json:

{
  "comment": "Check out developer.linkedin.com! http://linkd.in/1FC2PyG",
  "visibility": {
    "code": "anyone"
  }
}

这需要设置 http headers:

Content-Type: application/json
x-li-format: json

我正在尝试使用 OAuth.io 执行此操作,但 LinkedIn 似乎仍在阅读 XML 中的 body。

在此处查看我的示例:https://jsfiddle.net/Lv3jtpkb/2/

我从中得到以下错误:

Invalid xml {Element 'share@http://api.linkedin.com/v1' with element-only content type cannot have text content.}

我检查了从 OAuth.io 服务器到 LinkedIn API 端点的 headers 前端指定的 headers 是否以某种方式被更改并且可以确认它们没有被更改。

你快到了。有 2 个错误。

  1. 您的数据被格式化为 JS 对象。您使用的方法似乎没有自动将对象转换为 JSON。

  2. 这行代码没有做任何事情,并导致异常:

    .then(user => { console.log("next:",user) })
    

    删除它。

工作代码片段:

$('#linkedin-button').on('click', function() {
// Initialize with your OAuth.io app public key
OAuth.initialize('A5oDtHv-oUEVlR7q7hDolXxC7RE');
  alert('init');
  OAuth.popup('linkedin2').then(linkedin => {
  alert('logged in');
    linkedin.post({
      url: "/v1/people/~/shares?format=json",
      data: JSON.stringify({
        "comment": "Hello world!",
        "visibility": {
          "code": "anyone"
        }
      }),
      headers: {
         "x-li-format": "json",
         "Content-Type": "application/json"
      }
    }).then(data => {
      console.log("success:", data);
      }).fail(err => { console.log("err:",err) });
  })
})

控制台成功: