1

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

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 and Here'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

2
  • Any luck? I had this prob too. Commented Feb 4, 2015 at 19:48
  • no answer yet, tried many other codes but nothing worked, asked on google group still no answer Commented Feb 5, 2015 at 11:24

2 Answers 2

1

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

error & response

    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)
            }

        })

Hopefully that answers everything

Sign up to request clarification or add additional context in comments.

Comments

0

My app was crashing on the line

PFTwitterUtils.twitter().signRequest(req)

And the issue was that I was using a user who had recent been logged in with Twitter. Hope this helps.

1 Comment

@nickchuckwalter My app is also crashing on this line. How were you able to fix this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.