gpt4 book ai didi

swift - 使用 Swift 在 LinkedIn 上分享(无需 SDK)

转载 作者:行者123 更新时间:2023-11-30 11:05:08 25 4
gpt4 key购买 nike

虽然我见过很多可以在 LinkedIn 上共享的解​​决方案,但如果不使用 LISDKAPIHelper,我找不到任何解决方案。

这是我的场景。我有有效的 accessToken,我已经用它从用户配置文件中检索信息。以下是我一直尝试使用该 token 发布的步骤。我一直收到“statusCode = 400”。可能是我对 requestURL 做错了什么。

有人可以在这方面帮助我吗?

这是我的代码......

@IBAction func btnPostOn(_ sender: UIButton) {
print("btnPostOn pressed")


if let accessToken = UserDefaults.standard.object(forKey: "LIAccessToken") {
// Specify the URL string that we'll get the profile info from.
let targetURLString = "https://api.linkedin.com/v1/people/~/shares"
let payloadStr: String = "{\"comment\":\"Check out developer.linkedin.com!\",\"visibility\":{\"code\":\"anyone\"}}"

// Initialize a mutable URL request object.
let request = NSMutableURLRequest(url: NSURL(string: targetURLString)! as URL)

// Indicate that this is a GET request.
request.httpMethod = "POST"
request.httpBody = payloadStr.data(using: String.Encoding.utf8)
// Add the access token as an HTTP header field.
request.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")

// Make the request.
let task: URLSessionDataTask = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
// Get the HTTP status code of the request.
let statusCode = (response as! HTTPURLResponse).statusCode

if statusCode == 200 {
// Convert the received JSON data into a dictionary.

guard let json = (try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any] else {
print("Not containing JSON")
return
}


////To do////


}
}
task.resume()

}

}

最佳答案

最后我找到了适合我的解决方案。我做错的事情是制作我的请求 header 。它对我有用。

if let accessToken = UserDefaults.standard.object(forKey: "LIAccessToken") {
// Specify the URL string that we'll get the profile info from.
let targetURLString = "https://api.linkedin.com/v1/people/~/shares"
let payloadStr: String = "{\"comment\":\"Check out developer.linkedin.com!\",\"visibility\":{\"code\":\"anyone\"}}"

// Initialize a mutable URL request object.
let request = NSMutableURLRequest(url: NSURL(string: targetURLString)! as URL)

// Indicate that this is a GET request.
request.httpMethod = "POST"
request.httpBody = payloadStr.data(using: String.Encoding.utf8)
// Add the access token as an HTTP header field.
request.addValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("json", forHTTPHeaderField: "x-li-format")

// Make the request.
let task: URLSessionDataTask = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) -> Void in
// Get the HTTP status code of the request.
let statusCode = (response as! HTTPURLResponse).statusCode

if statusCode == 201 {
// Convert the received JSON data into a dictionary.

guard ((try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [String: Any]) != nil else {
print("Not containing JSON")
return
}

print("successfully posted.")
}
}
task.resume()

}

关于swift - 使用 Swift 在 LinkedIn 上分享(无需 SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52810350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com