2

I am new to iPhone development.I am using fabric for twitter login.For this am using this code.

[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession* session, NSError* error) {
        if (session)
        {
            NSLog(@"signed in as %@", [session userName]);
        }
        else
        {
            NSLog(@"error: %@", [error localizedDescription]);
        }
    }];

Now,i want to get user's First name and last name from twitter.I have done R&D on this and find some code Get user profile details (especially email address) from twitter in iOS But i am not unable to understand this code.Because it's give some error in my code. error is

Error: Error Domain=TwitterAPIErrorDomain Code=99 "Request failed: forbidden (403)" UserInfo={NSErrorFailingURLKey=https://api.twitter.com/oauth2/token, NSLocalizedDescription=Request failed: forbidden (403), NSLocalizedFailureReason=Twitter API error : Unable to verify your credentials (code 99)}
Community
  • 1
  • 1
sambit
  • 41
  • 5
  • 2
    Twitter accounts don't *have* a first and last name. There's just a single "name" field, which often isn't even the user's real name. –  Apr 11 '16 at 06:56
  • @duskwuff how can i get user's real name?? – sambit Apr 11 '16 at 07:23
  • Ask them? You're not going to find it out using Twitter. –  Apr 11 '16 at 15:54

2 Answers2

1

It might be too late to answer this question, but this might be helpful. Let's say you've already installed the Twitter SDK in you iOS app. You can get Twitter user's info like this:

1. Login with twitter:

Twitter.sharedInstance().logIn(completion: { (session, error) in }

2. Use the TWTRAPIClient to retrieve user's data:

Twitter.sharedInstance().logIn(completion: { (session, error) in 
    guard let session = session else {
        print("Something went wrong.")
        return
    }        

    let client = TWTRAPIClient()
    client.loadUser(withID: session.userID, completion: { (user, error) in
         print("user's name: \(user?.name ?? "")")
         print("user's profile picture: \(user?.profileImageURL ?? "")")
    })
}
Vasil Garov
  • 4,851
  • 1
  • 26
  • 37
  • This is largely correct, and maybe things have changed a bit since originally posted, but it doesn't work with the client assignment as you have it written above. The assignment should be: let client = TWTRAPIClient.withCurrentUser() – C6Silver Sep 13 '18 at 23:41
0
TWTRTwitter.sharedInstance().logIn { (twitterSession, error) in
    if let session = twitterSession {
        let client = TWTRAPIClient.withCurrentUser()
        client.loadUser(withID: session.userID, completion: { (twitterUser, userError) in
            if let user = twitterUser {
                print("user.name -> \(user.name)")
                print("user.profileImageURL -> \(user.profileImageURL)")
                print("user.profileURL -> \(user.profileURL)")
            } else {
                print("userError: \(String(describing: userError?.localizedDescription))");
            }
        })
    } else {
        print("error: \(String(describing: error?.localizedDescription))");
    }
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
honey
  • 91
  • 1
  • 4
  • Hi and welcome to SO. Can you please edit your answer and provide some description of your code? – rsm Feb 24 '18 at 18:54