Запрос электронной почты пользователя из 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, включая "электронную почту". Вот результат:
Электронная почта клиента Twitter - необязательно ("{\"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\"\ "объекты \":{\"URL \":{\"URLs\":[{\"URL \":\"https:\/\/t.co\/KOtATAEV3X\",\"expanded_url\":\"HTTP:\/\/Instagr.am\/danlevy114\",\"DISPLAY_URL \":\"Instagr.am\/danlevy114\",\"индексы \":[0,23]}]},\"описание \":{\"URLs\":[]}},\"защищенный \" ложь \"followers_count\":292,\"friends_count\":196,\" named_count \ ": 4, \" creation_at\":\"Sun Apr 22 15:20:46 +0000 2012\",\"favourites_count\":1151,\"utc_offset\":-10800,\"time_zone\":\"Атлантическое время (Канада)\",\"geo_enabled\": правда, \ "проверено \": ложно,\"statuses_count\":1305,\"lang\":\"ан \", \ "contributors_enabled \" ложь \ "is_translator \" ложь \ "is_translation_enabled \" ложь \"profile_background_color\":\"C0DEED\",\"profile_background_image_url\":\"HTTP:\/\/pbs.twimg.com\/profile_background_imag эс \/743634202\/69dd45bc569542274b017cc25c1e464d.png\",\"profile_background_image_url_https\":\"https:\/\/pbs.twimg.com\/profile_background_images\/743634202\/69dd45bc569542274b017cc25c1e464d.png\",\"profile_background_tile\": ложные, \ "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\"ложь \"has_extended_profile\": правда, \" default_profile \ "ложь \" default_profile_image \ "ложь \" после \ "ложь \" follow_request_sent\": ложные, \" уведомления \ ":ложный}")
Я вошел в систему с моей учетной записью Twitter, и электронное письмо не появилось. Я знаю, что у меня есть электронная почта, связанная с моей учетной записью Twitter. Есть идеи?
1 ответ
У меня была такая же проблема с твиттером. Прежде всего измените Разрешения в dev.twitter на "Только чтение".
Далее используйте этот код для входа в 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 и Секрет API в Firebase Console можно получить из dev.twitter.