在iOS中从Twitter获取用户个人资料详情(尤其是电子邮件地址)

3

我希望能够根据用户的Twitter账户获取用户的详细信息。首先,让我解释一下我想做什么。

在我的情况下,用户会被提供使用Twitter账户注册的选项。因此,基于用户的Twitter账户,我希望能够获取用户详情(例如电子邮件地址、姓名、个人资料图片、出生日期、性别等),并将这些详情保存在数据库中。现在,很多人可能会建议我使用ACAccountACAccountStore,这是一个提供接口用于访问、操作和存储账户的类。但在我的情况下,即使用户没有在iOS设置应用中配置Twitter账户,我也希望能够注册用户。我希望用户能够导航到Twitter的登录页面(在Safari中或在应用程序本身中,或者使用其他替代方式)。

我还参考了Twitter的API列表文档(在此处)。但我非常困惑该如何向用户展示登录屏幕以登录Twitter账户,以及如何获取个人资料信息。我应该使用UIWebView,还是将用户重定向到Safari,或采用其他方法呢?

5个回答

5

最终,在与长时间交谈后,我成功地使我的应用程序被白名单批准。以下是整个过程:

  • 向发送邮件,提供有关您的应用程序的一些详细信息,如Consumer key、应用商店中应用程序的链接、隐私政策链接、元数据以及登录我们应用程序的说明。在邮件中提到您想要在您的应用程序中访问用户的电子邮件地址。

  • 他们将审核您的应用程序,并在2-3个工作日内回复您。

  • 一旦他们说您的应用程序被批准加入白名单,就更新您的应用程序在Twitter Developer门户网站上的设置。登录apps.twitter.com并执行以下操作:

    1. 在“设置”选项卡上,添加服务条款和隐私政策URL
    2. 在“权限”选项卡上,将您的令牌范围更改为请求电子邮件。此选项仅在您的应用程序被批准加入白名单后才会出现。

开始编写代码:

同意Vizllx的说法:“Twitter为此提供了一个美丽的框架,您只需将其集成到您的应用程序中即可。”

获取用户电子邮件地址

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

获取用户配置文件
-(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);
    }
}

希望能有所帮助!!!

@NSPrtatik,我看到了你在不同帖子中的回答。你可否看看这个问题:http://stackoverflow.com/questions/32758067/whitelisted-ios-app-by-twitter-still-not-getting-email-of-user - Apple_Magic

4

如何在Twitter中获取电子邮件ID?

步骤1:进入https://apps.twitter.com/app/

步骤2:单击您的应用程序>单击权限选项卡。

步骤3:在此处勾选电子邮件框

enter image description here


3
在Twitter上,你只能获取用户的user_nameuser_id。相对于Facebook而言,Twitter不会提供像email idbirth dategender等信息,因此更加安全可靠。需要参考:link1

在 Twitter 的文档中,他们提到了获取用户电子邮件 ID 的方法。此外,我还得到了一些下载用户个人资料图片的示例。 - NSPratik
1
是的,当您传递 screen_name 后,我们可以获取用户图片,然后我们会获取该图片,在这里我附上了图片,请参考该链接。 - Anbu.Karthik
1
据我所知,在过去的8到9个项目中,我使用了Twitter API,但我们无法在Account框架中获取电子邮件ID,请参见此引用link2 - Anbu.Karthik
1
可能有一些第三方提供这个,我没有看到,如果你需要什么,我肯定会和你朋友一起帮忙。 - Anbu.Karthik
Anbu.Karthik,请检查我的答案是否正确。我已经可以获取用户的电子邮件地址了。 - NSPratik

3
Twitter提供了一个漂亮的框架,您只需要将其集成到您的应用程序中即可。

https://dev.twitter.com/twitter-kit/ios

它有一个简单的登录方法:

// Objective-C
TWTRLogInButton* logInButton =  [TWTRLogInButton
                                     buttonWithLogInCompletion:
                                     ^(TWTRSession* session, NSError* error) {
    if (session) {
         NSLog(@"signed in as %@", [session userName]);
    } else {
         NSLog(@"error: %@", [error localizedDescription]);
    }
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton];

这是获取用户个人资料信息的过程:
/* Get user info */
        [[[Twitter sharedInstance] APIClient] loadUserWithID:[session userID]
                                                  completion:^(TWTRUser *user,
                                                               NSError *error)
        {
            // handle the response or error
            if (![error isEqual:nil]) {
                NSLog(@"Twitter info   -> user = %@ ",user);
                NSString *urlString = [[NSString alloc]initWithString:user.profileImageLargeURL];
                NSURL *url = [[NSURL alloc]initWithString:urlString];
                NSData *pullTwitterPP = [[NSData alloc]initWithContentsOfURL:url];

                UIImage *profImage = [UIImage imageWithData:pullTwitterPP];


            } else {
                NSLog(@"Twitter error getting profile : %@", [error localizedDescription]);
            }
        }];

我认为你可以从Twitter Kit教程中找到其他信息,它还允许通过调用TwitterAuthClient#requestEmail方法并传入有效的TwitterSession和Callback来请求用户的电子邮件。


我们无法使用Twitter API获取电子邮件。Twitter不允许公开用户的电子邮件地址。 - NSPratik
1
谁说不可能,Twitter Kit 允许请求用户的电子邮件,调用 TwitterAuthClient#requestEmail 方法,传入有效的 TwitterSession 和 Callback。仔细阅读 Twitter Kit 文档,然后发表评论。 - Vizllx
我参考了许多帖子,这些帖子中说不可能实现,并且我也阅读了Twitter的文档。根据他们的文档,如果我们需要用户的电子邮件地址,则我们的应用程序应该被选入... - NSPratik
我决定使用Twitter kit。答案被接受了...一切都运行良好。只是我需要将我的应用程序列入Twitter的白名单,以请求用户的电子邮件地址。目前我得到的是_nil_作为电子邮件地址。 - NSPratik
@Pratik请检查第三个评论,获取电子邮件是可能的,您只需要仔细阅读文档。 - Vizllx
Vizllx,我在网上进行了大量探索,但无法找到调用TwitterAuthClient#requestEmail方法的方法,就像你建议的那样。此外,根据Twitter文档,我们将不得不填写一个表格在这里。但是没有请求用户电子邮件的选项。请帮帮我。我迫切需要解决方案。 - NSPratik

2
在Swift 4.2和Xcode 10.1中,它也可以获取电子邮件。
import TwitterKit 


@IBAction func onClickTwitterSignin(_ sender: UIButton) {

    TWTRTwitter.sharedInstance().logIn { (session, error) in
    if (session != nil) {
        let name = session?.userName ?? ""
        print(name)
        print(session?.userID  ?? "")
        print(session?.authToken  ?? "")
        print(session?.authTokenSecret  ?? "")
        let client = TWTRAPIClient.withCurrentUser()
        client.requestEmail { email, error in
            if (email != nil) {
                let recivedEmailID = email ?? ""
                print(recivedEmailID)
            }else {
                print("error--: \(String(describing: error?.localizedDescription))");
            }
        }
            //To get profile image url and screen name
            let twitterClient = TWTRAPIClient(userID: session?.userID)
                twitterClient.loadUser(withID: session?.userID ?? "") {(user, error) in
                print(user?.profileImageURL ?? "")
                print(user?.profileImageLargeURL ?? "")
                print(user?.screenName ?? "")
            }
        let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as!   SecondViewController
        self.navigationController?.pushViewController(storyboard, animated: true)
    }else {
        print("error: \(String(describing: error?.localizedDescription))");
    }
    }
}

跟随Harshil Kotecha的回答。
步骤1:转到https://apps.twitter.com/app/ 步骤2:点击您的应用程序>点击权限选项卡。
步骤3:在这里勾选电子邮件框。

enter image description here

如果您想退出登录。
let store = TWTRTwitter.sharedInstance().sessionStore
if let userID = store.session()?.userID {
    print(store.session()?.userID ?? "")
    store.logOutUserID(userID)
    print(store.session()?.userID ?? "")
    self.navigationController?.popToRootViewController(animated: true)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接