我尝试过多个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);
答案 0 :(得分: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,则电子邮件字段可能为空。发生这种情况时,完成块将传递错误,因为没有可用的电子邮件地址。
<强> PAST 强>
您可以通过否方式获取Twitter用户的电子邮件地址。
Twitter API不会将用户的电子邮件地址作为OAuth令牌协商过程的一部分提供,也不提供其他方式来获取它。
答案 1 :(得分:4)
您必须使用 Twitter框架。 Twitter为此提供了一个漂亮的框架,您只需将其集成到您的应用程序中即可。
要获取用户电子邮件地址,您的应用应列入白名单。这是link。转到use this form。您可以发送邮件到sdk-feedback@twitter.com,其中包含有关您的应用程序的一些详细信息,如消费者密钥,应用程序的App Store链接,隐私政策链接,元数据,如何登录我们的应用程序的说明等等。他们将在2-3个工作日内回复。
以下是我与Twitter支持团队交谈后如何列入白名单的故事:
向sdk-feedback@twitter.com
发送邮件,其中包含有关您的应用程序的一些详细信息,例如消费者密钥,应用程序的App Store链接,隐私政策链接,元数据,如何登录我们的应用程序的说明。在邮件中提到您要在应用程序内访问用户电子邮件地址。
他们会审核您的应用并在2-3个工作日内回复您。
一旦他们说您的应用已列入白名单,请在Twitter开发者门户网站中更新您的应用设置。登录apps.twitter.com并:
使用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 :(得分:2)
如果您想要用户的电子邮件地址,则需要在您自己的应用程序和服务范围内询问用户。 Twitter API不提供用户的电子邮件地址作为OAuth令牌协商过程的一部分,也不提供其他方法来获取它。
答案 3 :(得分: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))");
}
}
}
答案 4 :(得分:0)
@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)");
}
})
}