swift4: Callback URL not approved for this client

2019-04-26 19:20发布

i'm using swift4: i want to login to the app with twitter, but when i presses login with twitter button i got this error:

Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings" UserInfo={NSLocalizedDescription=Callback URL not approved for this client application. Approved callback URLs can be adjusted in your application settings} error: Optional("Request failed: forbidden (403)")

i'd followed this documentation and set all plist variables and app delegate also, this is my code in login page:

@IBAction func loginWithTwitter(_ sender: Any) {

    TWTRTwitter.sharedInstance().logIn(completion: {
        (session, error) in
        if let sess = session {
            print("session: ",sess.authToken, sess.authTokenSecret, sess.userID, sess.userName)
            self.loginWithTwitter(twitter_id: sess.userID, name: sess.userName)
        } else {
            print("error: ", error?.localizedDescription as Any)
        }
    })
}

func loginWithTwitter(twitter_id: String, name: String) {
    self.deviceMac = UIDevice.current.identifierForVendor!.uuidString
    KRProgressHUD.show(withMessage: "انتظر ....")
    API.loginWithTwitter(mac:self.deviceMac, twitter_id: twitter_id, name: name, token: self.token) { (error: Error?, success: Bool, value: Any) in
        if success {
            KRProgressHUD.dismiss()
            let json = JSON(value)
            print(json)
            if(json["token"] != "") {
                let token = json["token"].string
                let def = UserDefaults.standard
                def.set(token, forKey: "token")
                def.synchronize()

                Common.setIfTwitterLogin(login: token!)
                let homeViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController

                self.present(homeViewController, animated: false, completion: nil)
            } else {
                self.errorLable.text = "عذرا لقد حدث خطأ حاول مره أخري"
            }
        } else {
            KRProgressHUD.dismiss()
            self.errorLable.text = "عذرا لقد حدث خطأ حاول مره أخري"
            if Connectivity.isConnectedToNetwork(){
                print("Internet Connection Available!")
            }else{
                Common.showToast(messsage: "الرجاء التحقق من إتصالك بالإنترنت", view: self.view)
            }
        }
    }
}

and

class func loginWithTwitter(mac:String, twitter_id: String, name: String, token: String, completion: @escaping (_ error: Error?, _ success: Bool,_ value: Any) -> Void) {

    let loginURL = URLs.loginWithTwitter

    let loginParameters = [
        "mac": mac,
        "twitter_id": twitter_id,
        "token": token,
        "name": name]

    print(loginURL, loginParameters)
    Alamofire.request(loginURL, method: .post, parameters: loginParameters, encoding: URLEncoding.default, headers: nil)
        .responseJSON { reponse in
            switch reponse.result {
            case .failure(let error):
                print("error: ", error)
                completion(error, false, "")
            case .success(let value):
                completion(nil, true, value)
            }
    }
}

i'd set a call back url in twitter developer app setting, any one know what is the problem?

3条回答
可以哭但决不认输i
2楼-- · 2019-04-26 19:47

In your twitter dashboard Go to: https://apps.twitter.com Go into the settings tab and add following Callback URL.

twitterkit-<consumerKey>://

i.e. twitterkit-4bvXXXXXXNFfOXXwrXXXXXXmT://

查看更多
疯言疯语
3楼-- · 2019-04-26 20:00

Add twittersdk:// as one callback URL on your twitter app setting https://apps.twitter.com/ then try it. This worked for me.

查看更多
看我几分像从前
4楼-- · 2019-04-26 20:03

Twitter dev updated there rules, you can open your developer website,find the setting of Callback URLS ,add this one :

For android
twittersdk://

For iOS, please refer
https://stackoverflow.com/a/50850233/5740236
https://stackoverflow.com/a/50662575/5740236

and it will be work, wish to solve your problem

查看更多
登录 后发表回答