无法在swift中获取Twitter用户详细信息

时间:2015-10-21 13:25:27

标签: ios swift twitter

我正在尝试使用此代码加载Twitter用户详细信息

@IBAction func twitterLogin(sender: UIButton) {
    if SLComposeViewController.isAvailableForServiceType(SLServiceTypeTwitter) {

        let accountType = self.accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierTwitter)

        accountStore.requestAccessToAccountsWithType(accountType, options: nil, completion: { (granted : Bool, error : NSError?) -> Void in
            if error != nil {
                 print("Error in getting permission : \(error)")
            } else {
                 if granted {
                    let accounts : NSArray = self.accountStore.accountsWithAccountType(accountType)
                    if accounts.count > 0 {
                        self.twitterAccount = accounts.lastObject as? ACAccount

                        let url = NSURL(string: "https://api.twitter.com/1.1/users/show.json")

                        let parameters : NSDictionary = ["fields": "user_id,screen_name"]

                        let twitterRequest : SLRequest = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: url, parameters: parameters as [NSObject : AnyObject])
                        twitterRequest.account = self.facebookAccount
                        twitterRequest.performRequestWithHandler({ (responseData : NSData?, urlResponse : NSHTTPURLResponse?, error : NSError?) -> Void in

                        if error != nil {
                            print("Error : \(error)")
                        } else {
//                          print("data : \(responseData)")
                          if let response = responseData {
                            var dict = NSDictionary()
                            do {
                                dict = try! NSJSONSerialization.JSONObjectWithData(response, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                                } catch let error as NSError {
                                    print("Error : \(error)")
                                }
                                print(dict)
                           }
                        }
                        })
                        }
                    } else {
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            self.showAlert(nil, message: "Permission not granted")
                        })
                    }
                }
            })
        } else {
            self.askForSettingsChange("No Twitter Account", message: "There are no Twitter accounts configured. You can add or create Twitter account in Settings")
        }
    }

输出

{
    errors =     (
                {
            code = 215;
            message = "Bad Authentication data.";
        }
    );
}

获取许多示例,但无法获得正确的解决方案 我认为这可能是api通话中的问题,也可能是参数 尝试纠正但没有成功

1 个答案:

答案 0 :(得分:0)

Fabric是sdk,它通过访客或用户登录提供简单身份验证的Twitter登录。

Twitter login using Fabric

用Swift https://docs.fabric.io/ios/twitter/index.html

编写的示例代码

如果要对API使用原始REST调用,请按照开发人员文档中的OAuth详细信息进行操作

https://dev.twitter.com/oauth