本文介绍了使用 Parse.com Swift 调用 Twitter API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试使用 parse 的 sdk 和 swift 语言来使用 twitter api 调用.我能够获得用户的访问令牌、密码和屏幕名称,但我正在尝试调用 https://api.twitter.com/1.1/account/verify_credentials.json 并尝试返回正确的 json 数据,但它总是返回 0x0000000000000

I'm trying to use twitter api calls using parse's sdk with swift language. I was able to get the user's access token, secret and screen name but I'm trying to call https://api.twitter.com/1.1/account/verify_credentials.json and trying return the correct json data but it is always returning 0x0000000000000

这是我正在尝试使用的 Parse.com 指南,它位于 Objective C https://parse.com/docs/ios_guide#twitterusers-requests/iOS 和这是我的 Swift 代码

here is the Parse.com Guide which I'm trying to use which is in Objective C https://parse.com/docs/ios_guide#twitterusers-requests/iOS andHere's my Swift code

if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()) {

    var token : NSString = PFTwitterUtils.twitter().authToken
    var secret : NSString = PFTwitterUtils.twitter().authTokenSecret
    var usern : NSString = PFTwitterUtils.twitter().screenName
    var credential : ACAccountCredential = ACAccountCredential(OAuthToken: token, tokenSecret: secret)

    var verify : NSURL = NSURL(string: "https://api.twitter.com/1.1/account/verify_credentials.json")!

    var req:NSMutableURLRequest = NSMutableURLRequest(URL: verify)
    req.HTTPMethod = "GET"
    PFTwitterUtils.twitter().signRequest(req)
    var response:AutoreleasingUnsafeMutablePointer<NSURLResponse?> = nil
    var error:NSError? = nil

    var data:NSData = NSURLConnection.sendSynchronousRequest(req, returningResponse: response, error: &error)! as NSData

    if error != nil {
        println(error)
    } else {
        println(response)
    }

}

我不确定我在哪里做错了,因为它总是返回

I'm not sure where I'm doing this wrong because it is always returning

0x0000000000000

推荐答案

我不确定您要查找什么,但从描述来看,您似乎希望从 Twitter 返回 JSON.这应该在您的数据变量中.无论如何,这里是所有问题的答案,以防您还想要回复.

I'm not exactly sure what you are looking for but from the description it looks like you want the JSON back from Twitter. This should be in your data variable. Anyway here's an answer to everything in case you also want the response back.

    //Synchronous version
    if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()) {

        var token : NSString = PFTwitterUtils.twitter().authToken
        var secret : NSString = PFTwitterUtils.twitter().authTokenSecret
        var usern : NSString = PFTwitterUtils.twitter().screenName

        var credential : ACAccountCredential = ACAccountCredential(OAuthToken: token, tokenSecret: secret)
        var verify : NSURL = NSURL(string: "https://api.twitter.com/1.1/account/verify_credentials.json")!
        var request : NSMutableURLRequest = NSMutableURLRequest(URL: verify)

        //You don't need this line
        //request.HTTPMethod = "GET"

        PFTwitterUtils.twitter().signRequest(request)

        //Using just the standard NSURLResponse.
        var response: NSURLResponse? = nil
        var error: NSError? = nil
        var data = NSURLConnection.sendSynchronousRequest(request,
            returningResponse: &response, error: nil) as NSData?

        if error != nil {
            println("error \(error)")
            } else {
               //This will print the status code repsonse. Should be 200.
               //You can just println(response) to see the complete server response
               println((response as NSHTTPURLResponse).statusCode)
               //Converting the NSData to JSON
               let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data!,
                options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
               println(json)
            }
        }

您也可以异步执行此操作.用这个替换最后一部分,你也不需要这两个变量

You can also do this asynchronously. Replace the last section with this and you also don't need the two variables

错误&回复

    let queue : NSOperationQueue = NSOperationQueue()
        NSURLConnection.sendAsynchronousRequest(request, queue: queue,
            completionHandler:{(response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
            if error != nil {
                println("error \(error)")
            } else {
                println((response as NSHTTPURLResponse).statusCode)
                let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data,
                    options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
                println(json)
            }

        })

希望能回答一切

这篇关于使用 Parse.com Swift 调用 Twitter API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:34