Neeraj Joshi
Neeraj Joshi

Reputation: 769

Unable to fetch user's email from twitter using twitter fabric iOS framework

I am trying my hands on new fabric twitter kit for iOS. After signing in we can ask user to allow access for the email id and if it allows then returns email of the logged in user but it giving me error.

Email (null), Error: Error Domain=NSURLErrorDomain Code=-1001 
"The request timed out." UserInfo=0x7fdd314f1c30 
{NSUnderlyingError=0x7fdd314d9aa0 "The request timed out.", 
NSErrorFailingURLStringKey=https://api.twitter.com/1.1/account/verify_cre
dentials.json?skip_status=true&include_email=true, 
NSErrorFailingURLKey=https://api.twitter.com/1.1/account/verify_credentia
ls.json?skip_status=true&include_email=true, 
NSLocalizedDescription=The request timed out.}

And here is my code that i've tried from their doc.

if ([[Twitter sharedInstance] session]) {
    TWTRShareEmailViewController* shareEmailViewController =
    [[TWTRShareEmailViewController alloc]
     initWithCompletion:^(NSString* email, NSError* error) {
         NSLog(@"Email %@, Error: %@", email, error);
     }];
    [self presentViewController:shareEmailViewController
                       animated:YES
                     completion:nil];
} else {
    // TODO: Handle user not signed in (e.g.
    // attempt to log in or show an alert)
}

Is anything wrong in my code? Please help me. I can post my status and media to twitter but can't get email Id.

Can anyone please help me in this problem? I'm also new to development.

Upvotes: 2

Views: 1380

Answers (1)

NSPratik
NSPratik

Reputation: 4836

To get user email address, your application should be whitelisted. Here is the link. Go to use this form. You can either send mail to [email protected] with some details about your App like Consumer key, App Store link of an App, Link to privacy policy, Metadata, Instructions on how to log into our App etc..They will respond within 2-3 working days.

Here is the story how I got whitelisted by conversation with Twitter support team:

  • Send mail to [email protected] with some details about your App like Consumer key, App Store link of an App, Link to privacy policy, Metadata, Instructions on how to log into our App. Mention in mail that you want to access user email adress inside your App.

  • They will review your App and reply to you withing 2-3 business days.

  • Once they say that your App is whitelisted, update your App's settings in Twitter Developer portal. Sign in to apps.twitter.com and:

    1. On the 'Settings' tab, add a terms of service and privacy policy URL
    2. On the 'Permissions' tab, change your token's scope to request email. This option will only been seen, once your App gets whitelisted.

Put your hands on code

Use of Twitter framework:

Get user email address

-(void)requestUserEmail
    {
        if ([[Twitter sharedInstance] session]) {

            TWTRShareEmailViewController *shareEmailViewController =
            [[TWTRShareEmailViewController alloc]
             initWithCompletion:^(NSString *email, NSError *error) {
                 NSLog(@"Email %@ | Error: %@", email, error);
             }];

            [self presentViewController:shareEmailViewController
                               animated:YES
                             completion:nil];
        } else {
            // Handle user not signed in (e.g. attempt to log in or show an alert)
        }
    }

Get user profile

-(void)usersShow:(NSString *)userID
{
    NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/users/show.json";
    NSDictionary *params = @{@"user_id": userID};

    NSError *clientError;
    NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
                             URLRequestWithMethod:@"GET"
                             URL:statusesShowEndpoint
                             parameters:params
                             error:&clientError];

    if (request) {
        [[[Twitter sharedInstance] APIClient]
         sendTwitterRequest:request
         completion:^(NSURLResponse *response,
                      NSData *data,
                      NSError *connectionError) {
             if (data) {
                 // handle the response data e.g.
                 NSError *jsonError;
                 NSDictionary *json = [NSJSONSerialization
                                       JSONObjectWithData:data
                                       options:0
                                       error:&jsonError];
                 NSLog(@"%@",[json description]);
             }
             else {
                 NSLog(@"Error code: %ld | Error description: %@", (long)[connectionError code], [connectionError localizedDescription]);
             }
         }];
    }
    else {
        NSLog(@"Error: %@", clientError);
    }
}

Hope it helps !!!

Upvotes: 1

Related Questions