【问题标题】:Sharing on LinkedIn using Swift (Without SDK)使用 Swift 在 LinkedIn 上分享(无 SDK)
【发布时间】:2018-10-15 05:43:38
【问题描述】:

虽然我在 LinkedIn 上看到过许多有效的共享解决方案,但如果不使用 LISDKAPIHelper,我找不到任何解决方案。

这是我的场景。 我有我的有效 accessToken,我已经用它从用户个人资料中检索信息。以下是我尝试使用该令牌发布的步骤。我一直收到'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()

    }

}

【问题讨论】:

  • 正如我所说,这些解决方案使用了 LISDKAPIHelper。我提到我需要一个不使用 SDK lib 的解决方案。但是,我找到了我的解决方案。谢谢。 :)

标签: swift oauth share linkedin


【解决方案1】:

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

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()

    }

【讨论】:

    猜你喜欢
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多