从 Twitter 请求用户的电子邮件

Requesting user's email from Twitter

我正在尝试从 Twitter 请求用户的电子邮件地址并将其放入 Firebase。我的应用程序已被 Twitter 列入白名单,并在 apps.twitter.com 门户中激活了它。根据这里的 Fabric 文档,我写了这段代码:

let twitterEmailClient = TWTRAPIClient.clientWithCurrentUser()
let twitterEmailRequest = twitterEmailClient.URLRequestWithMethod("GET", URL: "https://api.twitter.com/1.1/account/verify_credentials.json", parameters: ["include_email": "true", "skip_status": "true"], error: nil)
twitterEmailClient.sendTwitterRequest(twitterEmailRequest, completion: { (TWTREmailClientResponse: NSURLResponse?, TWTREmailClientEmail: NSData?, TWTREmailClientError: NSError?) in

    if TWTREmailClientError != nil {

        print("Twitter Email Client Error - \(TWTREmailClientError!.code): \(TWTREmailClientError!.localizedDescription)")

    } else if TWTREmailClientResponse == nil {

        print("Twitter Email Client Error - valid connection not available")

    } else if TWTREmailClientEmail != nil {

        print("Twitter Client Email - \(String(data: TWTREmailClientEmail!, encoding: NSUTF8StringEncoding))")

        FIRAuth.auth()?.currentUser?.updateEmail("\(TWTREmailClientEmail)", completion: { (updateEmailError: NSError?) in

            if updateEmailError != nil {

                print("Set Email from Twitter Error - \(updateEmailError)")

            }
        })
    }
})

同样,根据 Fabric 的文档,我应该得到一个 JSON 结果,包括 'email'。这是结果:

Twitter Client Email - Optional("{\"id\":560366005,\"id_str\":\"560366005\",\"name\":\"Dan Levy\",\"screen_name\":\"DanLevy114\",\"location\":\"Buffalo, NY\",\"description\":\"Florida Tech \'20, Amherst \'16, iOS Developer\",\"url\":\"https:\/\/t.co\/KOtATAEV3X\",\"entities\":{\"url\":{\"urls\":[{\"url\":\"https:\/\/t.co\/KOtATAEV3X\",\"expanded_url\":\"http:\/\/Instagr.am\/danlevy114\",\"display_url\":\"Instagr.am\/danlevy114\",\"indices\":[0,23]}]},\"description\":{\"urls\":[]}},\"protected\":false,\"followers_count\":292,\"friends_count\":196,\"listed_count\":4,\"created_at\":\"Sun Apr 22 15:20:46 +0000 2012\",\"favourites_count\":1151,\"utc_offset\":-10800,\"time_zone\":\"Atlantic Time (Canada)\",\"geo_enabled\":true,\"verified\":false,\"statuses_count\":1305,\"lang\":\"en\",\"contributors_enabled\":false,\"is_translator\":false,\"is_translation_enabled\":false,\"profile_background_color\":\"C0DEED\",\"profile_background_image_url\":\"http:\/\/pbs.twimg.com\/profile_background_images\/743634202\/69dd45bc569542274b017cc25c1e464d.png\",\"profile_background_image_url_https\":\"https:\/\/pbs.twimg.com\/profile_background_images\/743634202\/69dd45bc569542274b017cc25c1e464d.png\",\"profile_background_tile\":false,\"profile_image_url\":\"http:\/\/pbs.twimg.com\/profile_images\/745047852796289024\/BWFfrEoI_normal.jpg\",\"profile_image_url_https\":\"https:\/\/pbs.twimg.com\/profile_images\/745047852796289024\/BWFfrEoI_normal.jpg\",\"profile_banner_url\":\"https:\/\/pbs.twimg.com\/profile_banners\/560366005\/1466468226\",\"profile_link_color\":\"0084B4\",\"profile_sidebar_border_color\":\"FFFFFF\",\"profile_sidebar_fill_color\":\"33FF33\",\"profile_text_color\":\"E05151\",\"profile_use_background_image\":false,\"has_extended_profile\":true,\"default_profile\":false,\"default_profile_image\":false,\"following\":false,\"follow_request_sent\":false,\"notifications\":false}")

我使用我的 Twitter 帐户登录,但没有出现电子邮件。我知道我有一个与我的 Twitter 帐户关联的电子邮件。有什么想法吗?

我在 Twitter 上遇到了同样的问题。首先将 dev.twitter 中的权限更改为 "Read only"。

接下来,使用此代码登录 Twitter:

Twitter.sharedInstance().logInWithCompletion() { session, error in
        if let session = session {

            let credential = FIRTwitterAuthProvider.credentialWithToken(session.authToken, secret: session.authTokenSecret)

            FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
                if let error = error {
                    Alert.sharedInstance.showAlert("Error", message: error.localizedDescription)
                    return
                }
                //Logged!
            }
        } else {
            Alert.sharedInstance.showAlert("Error", message: error!.localizedDescription)
        }
    }

这项工作无需向 Twitter 提出请求。 API Firebase 控制台中的密钥和 API 密钥来自 dev.twitter。