使用Swift在LinkedIn上共享(不带SDK)

时间:2018-10-15 05:43:38

标签: swift oauth share linkedin

尽管我看到了许多可以在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()

    }

}

1 个答案:

答案 0 :(得分:0)

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

    }