簡體   English   中英

從iOS中的Twitter獲取用戶個人資料詳細信息(尤其是電子郵件地址)

[英]Get user profile details (especially email address) from Twitter in iOS

我的目標是根據他/她的Twitter帳戶獲取用戶的詳細信息。 首先,讓我解釋一下我想做什么。

在我的情況下,將向用戶顯示使用Twitter帳戶注冊的選項。 因此,基於用戶的Twitter帳戶,我希望能夠獲取用戶詳細信息(例如電子郵件ID,姓名,個人資料圖片,出生日期,性別等)並將這些詳細信息保存在數據庫中。 現在,很多人可能會建議我使用ACAccountACAccountStore ,這是一個提供訪問,操作和存儲帳戶的界面的類。 但在我的情況下,即使用戶尚未在iOS設置應用中為Twitter配置帳戶,我也想要注冊用戶。 我希望用戶導航到Twitter的登錄屏幕(在Safari或App本身,或使用任何其他替代方案)。

我也提到Twitter的API有列表的文檔在這里 但我很困惑應該如何向用戶提供登錄屏幕以登錄Twitter帳戶以及如何獲取個人資料信息。 我應該使用UIWebView ,還是將用戶重定向到Safari或采用其他方式?

最后,在與sdk-feedback@twitter.com進行了長時間的對話sdk-feedback@twitter.com ,我將我的應用程序列入白名單。 這是故事:

  • 發送郵件至sdk-feedback@twitter.com ,其中包含有關您的應用程序的一些詳細信息,如消費者密鑰,應用程序的App Store鏈接,隱私政策鏈接,元數據,如何登錄我們的應用程序的說明。 在郵件中提到您要訪問應用程序內的用戶電子郵件地址。

  • 他們將審核您的應用程序並在2-3個工作日內回復您。

  • 一旦他們說您的應用程序被列入白名單,請在Twitter開發人員門戶中更新您的應用程序設置。 登錄apps.twitter.com並:

    1. 在“設置”標簽上,添加服務條款和隱私權政策網址
    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);
    }
}

希望能幫助到你 !!!

如何在Twitter中獲取電子郵件ID?

第1步:到https://apps.twitter.com/app/

第2步:點擊你的應用>點擊權限標簽。

第3步:在這里查看郵箱

在此輸入圖像描述

在Twitter中,您只能獲得user_nameuser_id 您無法獲取email idbirth dategender等,這是非常安全的。與Facebook相比,Twitter對提供數據非常保密。

需要參考: link1

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 Tutorial中找到休息,它還允許通過調用TwitterAuthClient#requestEmail方法請求用戶的電子郵件,傳入有效的TwitterSession和Callback。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM