ios - 使用 twitter swift 4 登录时出错

标签 ios swift twitter authentication twitter-login

我想用 twitter 登录,但出现此错误

Request failed: forbidden (403)

只有当我的手机上没有 Twitter 应用程序但如果我的手机上有 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)");
        }
    }

}

最佳答案

请检查在您的应用程序的设置菜单上的 Twitter 开发人员门户中分配的回调 URL 是什么。它必须采用 twitterkit-apiKey:// 格式。如果你有像 abc 这样的 apiKey,你应该添加 twitterkit-abc://

关于ios - 使用 twitter swift 4 登录时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51423248/

相关文章:

Java 推特 API

python - 多次尝试后无法访问推特

Java UTF-16 到 UTF-8 的转换

iphone - 如何知道 iOS 中的 UITextField 是否有空格

ios - 视网膜屏幕的图像 (@2x)

ios - 最佳实践 : Access properties

ios - UICollectionView 嵌入在 UITableViewCell 中!无法从 tableview 单元格中呈现 viewcontroller!回电

ios - SpriteKit 和 Swift : Creating nodes via didBeginContact messes up the positioning

ios - 从 Swift 访问 extern const struct

objective-c - 在 swift 项目中使用 Objective C 文件而不使用 use_frameworks 标志