【问题标题】:Get user profile details (especially email address) from Twitter in iOS在 iOS 中从 Twitter 获取用户个人资料详细信息(尤其是电子邮件地址)
【发布时间】:2015-07-25 22:22:39
【问题描述】:

我的目标是根据他/她的 Twitter 帐户获取用户的详细信息。现在首先,让我解释一下我想要做什么。

在我的例子中,用户将看到一个选项以使用 Twitter 帐户注册。因此,基于用户的 Twitter 帐户,我希望能够获取用户详细信息(例如电子邮件 ID、姓名、头像、出生日期、性别等)并将这些详细信息保存在数据库中。现在,很多人可能会建议我使用ACAccountACAccountStore,这是一个提供用于访问、操作和存储帐户的接口的类。但就我而言,我想注册用户,即使用户没有在 iOS 设置应用程序中为 Twitter 配置帐户。我希望用户导航到 Twitter 的登录屏幕(在 Safari 或应用程序本身中,或使用任何其他替代方法)。

我还参考了具有 API 列表 here 的 Twitter 文档。但我很困惑如何向用户显示登录屏幕以登录 Twitter 帐户以及如何获取个人资料信息。我应该使用UIWebView,还是将用户重定向到 Safari 或采用其他方式?

【问题讨论】:

    标签: ios objective-c twitter twitter-oauth acaccountstore


    【解决方案1】:

    最后,在与sdk-feedback@twitter.com 进行了长时间的交谈后,我将我的应用程序列入了白名单。故事如下:

    • 发送邮件至sdk-feedback@twitter.com,提供有关您的应用程序的一些详细信息,例如消费者密钥、应用程序的应用程序商店链接、隐私政策链接、元数据、有关如何登录我们的应用程序的说明。在邮件中提及您要在应用内访问用户电子邮件地址。

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

    • 一旦他们说您的应用已列入白名单,请在 Twitter 开发者门户中更新您的应用设置。登录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);
        }
    }
    

    希望对你有帮助!!!

    【讨论】:

    【解决方案2】:

    如何在 twitter 中获取 email id ?

    第一步:到达https://apps.twitter.com/app/

    第 2 步:点击您的应用 > 点击权限选项卡。

    第 3 步:这里检查邮箱

    【讨论】:

      【解决方案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 工具包教程中找到,它还允许通过调用 TwitterAuthClient#requestEmail 方法,传入有效的 TwitterSession 和回调来请求用户的电子邮件。

      【讨论】:

      • 我们无法使用 Twitter API 接收电子邮件。 Twitter 不允许暴露用户的电子邮件地址。
      • 谁说不可能,Twitter Kit 允许请求用户的电子邮件,调用 TwitterAuthClient#requestEmail 方法,传入有效的 TwitterSession 和回调。仔细阅读 Twitter Kit 文档,然后发表评论。
      • 我参考了很多帖子说这是不可能的,我还阅读了 Twitter 的文档。根据他们的文档,如果我们需要用户电子邮件地址,那么到那时我们的应用程序应该被列入候选名单......
      • 我决定使用 Twitter 工具包。接受答案...一切正常。只是我必须在 Twitter 上将我的应用程序列入白名单,以请求用户他/她的电子邮件地址。目前我收到 nil 作为电子邮件地址。
      • @Pratik 检查第三条评论,电子邮件提取是可能的,你只需要仔细阅读文档。
      【解决方案4】:

      在 Twitter 中,您只能获取 user_nameuser_id。您无法获取email idbirth dategender 等,这非常安全,与 Facebook 相比,Twitter 在提供数据方面非常保密。

      需要参考:link1.

      【讨论】:

      • 在 Twitter 的文档中,他们提到了获取用户电子邮件 id 的方法。我也有一些例子来下载用户个人资料图片。
      • 是的,当你通过 screen_name 之后我们可以获取用户图片,然后我们获取图像,在这里我附上图像,请参考该链接
      • 据我所知,我在过去的 8 个或 9 个项目中使用了 twitter api,我们无法在 Account 框架中获取电子邮件 ID,请参阅此参考 link2
      • 可能是一些第三方提供的,我没看到,如果你需要什么我当然希望你的朋友
      • Anbu.Karthik,请检查我添加的答案。这是可能的,我现在可以获取用户的电子邮件地址。
      【解决方案5】:

      在 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 步:这里检查邮箱

      如果你想退出

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-13
        • 2011-12-01
        • 1970-01-01
        • 2011-03-08
        • 1970-01-01
        • 2019-06-15
        • 2017-12-28
        相关资源
        最近更新 更多