首页 > 解决方案 > 使用 twitter swift 4 登录时出错

问题描述

我想用 twitter 登录,但我收到了这个错误

请求失败:禁止 (403)

仅当我的手机上没有 twitter 应用程序但如果我的手机上有 twitter 应用程序它可以正常登录时,当我使用 twitter 登录时会发生此错误。

所以为什么我会收到这个错误。

我将此代码用于推特。

@IBAction func twtBtnPressed(_ sender: Any) {

    if !Connectivity.isConnectedToInternet() {
        Helper.instance.showAlert(self, title: "no conection ", message: "Please check your internet connection", buttonTitle: "Ok", buttonAction: nil)
    }else{
        Helper.instance.startIndicator2(self, "show", self.lang!, hudd)
        print("twitter btn Pressed")
        LoginVC.type = "T"

        Helper.instance.fetchDataFromTwt(completion: { (id, name, email, image) in
            if (id != nil && name != nil && email != nil) {
                print("Log in successfully")
                print("Login successfully")
                print(id!,name!,email!)
                APIHelper.apiCore.login_Social(email!, name!, self.self.lang!, "\(id!)","twitter", completion: { (code, message) in
                    if code == 200 {
                        print("Api_token: \((message)!)")
                        Helper.instance.saveApiToken(token: message!)
                        Helper.instance.startIndicator2(self, "showw", self.self.lang!, hudd)
                        ProfileVC.twtImage = image
                        self.performSegue(withIdentifier: "ProfileVC", sender: nil)

                    }else{
                        print("Error: \(String(describing: message))")
                        Helper.instance.showAlert(self, title: "Error", message: message!, buttonTitle: "Ok", buttonAction: { (action) in
                            Helper.instance.startIndicator2(self, "showw", self.self.lang!, hudd)
                        })

                    }
                })
            }
        })

    }

}

并从 twitter 应用程序中获取数据,我使用此代码。

func fetchDataFromTwt(completion: @escaping (_ id:Int?,_ name:String?,_ email: String?,_ image: String?)-> Void){

    TWTRTwitter.sharedInstance().logIn { (session, error) in

        if (session != nil) {

            print("signed in as \(session!.userName)");
            let client = TWTRAPIClient.withCurrentUser()

            let request = client.urlRequest(withMethod: "GET", urlString: "https://api.twitter.com/1.1/account/verify_credentials.json", parameters: ["include_entities": "false", "include_email": "true", "skip_status": "true"], error: nil)

            client.sendTwitterRequest(request) { response, data, connectionError in

                if connectionError != nil {
                    print("Error: \(String(describing: connectionError))")

                }else{
                    do {
                        let twitterJson = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:AnyObject]

                        let name = twitterJson["name"]
                        let id = twitterJson["id"]
                        let email = twitterJson["email"]
                        let image = twitterJson["profile_image_url_https"]

                        print(name!,id!,email!,image!)

                        completion(id as? Int, name as? String, email as? String, image as? String)

                    } catch let jsonError as NSError {
                        print("json error: \(jsonError.localizedDescription)")

                    }
                }

            }

        } else {
            print("error: \(error!.localizedDescription)");
        }
    }

}

标签: iosswifttwitterlogintwitter-login

解决方案


请检查在您的应用程序的设置菜单上的 twitter 开发人员门户中分配的回调 url 是什么。它必须是格式twitterkit-apiKey://。如果您有 abc 之类的 apiKey,则应添加twitterkit-abc://.


推荐阅读