如何通过iOS中的Twitter API获取用户电子邮件地址?

问题描述 投票:6回答:4

我尝试了多个SDK,但无法从任何资源中获取电子邮件ID。我为此目的尝试了FHSTwitterEngine,但我没有得到解决方案。

FHSTwitterEngine *twitterEngine = [FHSTwitterEngine sharedEngine];
NSString *username = [twitterEngine loggedInUsername]; //self.engine.loggedInUsername;
NSString *key = [twitterEngine accessToken].key;
NSString *secrete = [twitterEngine accessToken].secret;

if (username.length > 0)
{
    NSDictionary *userProfile = [[FHSTwitterEngine sharedEngine] getProfileUsername:username];
    NSLog(@"userProfile: %@", userProfile);
iphone ios5 twitter
4个回答
13
投票

编辑

在Twitter更新了API之后,现在用户可以使用TWTRShareEmailViewController类获取电子邮件。

// Objective-C
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)
}

// Swift
if Twitter.sharedInstance().session {
  let shareEmailViewController = TWTRShareEmailViewController() { email, error in
    println("Email \(email), Error: \(error)")
  }
  self.presentViewController(shareEmailViewController, animated: true, completion: nil)
} else {
  // TODO: Handle user not signed in (e.g. attempt to log in or show an alert)
}

注意:即使用户授予访问其电子邮件地址的权限,也不保证您将获得电子邮件地址。例如,如果有人使用电话号码而不是电子邮件地址注册Twitter,则电子邮件字段可能为空。发生这种情况时,完成块将传递错误,因为没有可用的电子邮件地址。

Twitter Dev Ref


过去

没有办法可以获得Twitter用户的电子邮件地址。

Twitter API不提供用户的电子邮件地址作为OAuth令牌协商过程的一部分,也不提供其他方法来获取它。

Twitter Doc


4
投票

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

要获取用户电子邮件地址,您的应用程序应列入白名单。这是link。去use this form。您可以发送邮件到[email protected],其中包含有关您的应用程序的一些详细信息,如消费者密钥,应用程序的App Store链接,隐私政策链接,元数据,如何登录我们的应用程序的说明等等。他们将在2-3个工作日内回复。

以下是我与Twitter支持团队交谈后如何列入白名单的故事:

  • 发送邮件给[email protected],其中包含有关您的应用程序的一些详细信息,如消费者密钥,应用程序的App Store链接,隐私政策链接,元数据,如何登录我们的应用程序的说明。在邮件中提到您要在应用程序内访问用户电子邮件地址。
  • 他们将审核您的应用程序并在2-3个工作日内回复您。
  • 一旦他们说您的应用程序被列入白名单,请在Twitter开发人员门户中更新您的应用程序设置。登录apps.twitter.com并: 在“设置”标签上,添加服务条款和隐私权政策网址 在“权限”标签上,将令牌的范围更改为请求电子邮件。只有在您的应用程序列入白名单后,才会看到此选项。

Put your hands on code

使用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);
    }
}

希望能帮助到你 !!!


2
投票

如果您想要用户的电子邮件地址,则需要在您自己的应用程序和服务范围内询问用户。 Twitter API不提供用户的电子邮件地址作为OAuth令牌协商过程的一部分,也不提供其他方法来获取它。


0
投票

在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))");
            }
        }
        let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "SVC") as! SecondViewController
        self.navigationController?.pushViewController(storyboard, animated: true)
    }else {
        print("error: \(String(describing: error?.localizedDescription))");
    }
}
}

0
投票

Swift 3-4

@IBAction func btnTwitterAction(_ sender: Any) {

        TWTRTwitter.sharedInstance().logIn(completion: { (session, error) in
            if (session != nil) {
                print("signed in as \(String(describing: session?.userName))");
                if let mySession = session{

                    let client = TWTRAPIClient.withCurrentUser()
                    //To get User name and email
                    client.requestEmail { email, error in
                        if (email != nil) {
                            print("signed in as \(String(describing: session?.userName))");
                            let firstName = session?.userName ?? ""   // received first name
                            let lastName = session?.userName ?? ""  // received last name
                            let recivedEmailID = email ?? ""   // received email

                        }else {
                            print("error: \(String(describing: error?.localizedDescription))");
                        }
                    }


                    //To get user profile picture
                    client.loadUser(withID: session?.userID, completion: { (userData, error) in
                        if (userData != nil) {

                            let fullName = userData.name //Full Name
                            let userProfileImage = userData.profileImageLargeURL //User Profile Image
                            let userTwitterProfileUrl = userData?.profileURL // User TwitterProfileUrl
                        }
                    })
                }
            } else {
                print("error: \(error?.localizedDescription)");
            }
        })

    }
© www.soinside.com 2019 - 2024. All rights reserved.