简体   繁体   中英

share post on linkedin using ios swift 3 and linkedin sdk is not working

I am using the following code to share the post in linked in using swift 3 but it is giving the following error.

Code

func shareOnLinkedIn(){
    LISDKSessionManager.createSession(withAuth:
        [LISDK_BASIC_PROFILE_PERMISSION,LISDK_W_SHARE_PERMISSION], state: nil, showGoToAppStoreDialog: true, successBlock: {(sucess) in
            let session = LISDKSessionManager.sharedInstance().session
            print(session?.description)
            //let url = "https://api.linkedin.com/v1/people/~"

            LISDKAPIHelper.sharedInstance()?.getRequest("https://api.linkedin.com/v1/people/~", success: { (response) in

                if let response = response{
                    let data = response.data.data(using: .utf8)
                    let dictResponse = try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSDictionary

                    DispatchQueue.main.async {
                        if LISDKSessionManager.hasValidSession(){
                            let url: String = "https://api.linkedin.com/v1/people/~/shares"

                            let payloadStr: String = "{\"comment\":\"YOUR_APP_LINK_OR_WHATEVER_YOU_WANT_TO_SHARE\",\"visibility\":{\"code\":\"anyone\"}}"

                            let payloadData = payloadStr.data(using: String.Encoding.utf8)


                            LISDKAPIHelper.sharedInstance().postRequest(url, body: payloadData, success: { (response) in
                                print(response!.data)
                            }, error: { (error) in
                                print(error!)

                                let alert = UIAlertController(title: "Alert!", message: "something went wrong", preferredStyle: .alert)
                                let action = UIAlertAction(title: "OK", style: .default, handler: nil)

                                alert.addAction(action)
                                self.present(alert, animated: true, completion: nil)
                            })
                        }
                    }

                }

            }, error: { (error) in
                print(error?.localizedDescription as Any)
            })


    }) {(error) in
        print("Error \(String(describing: error))")
    }
}

Error

Error Domain=LISDKErrorAPIDomain Code=400 "(null)" UserInfo={LISDKAuthErrorAPIResponse=}

Note: I tried this solution but doesn't help. Share on Linkedin using the SDK for iOS doesn't work

Found the solution,

The problem is with the payload which I am sending. Use the following payload instead of using the above.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM