我正在尝试从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的文档,我应该得到一个包含'email'的JSON结果。结果如下:
Twitter客户端电子邮件 - 可选( “{\” ID \ “:560366005,\” ID_STR \ “:\” 560366005 \ “\ ”名称\“:\” 丹 利维\ “\ ”SCREEN_NAME \“:\ ”DanLevy114 \“,\ ”位置\“:\” 布法罗, NY \“,\”description \“:\”Florida Tech \ '20,Amherst \ '16,iOS 开发者\”,\ “URL \”:\ “HTTPS:\ / \ / t.co \ / KOtATAEV3X \”,\ “实体\”:{\ “URL \”:{\ “的url \”:[{\ “URL \”:\ “HTTPS:\ / \ / t.co \ / KOtATAEV3X \”,\ “expanded_url \”:\ “HTTP:\ / \ / Instagr.am \ / danlevy114 \”,\ “DISPLAY_URL \” :\ “Instagr.am \ / danlevy114 \” \ “\指数”:[0,23]}]},\ “描述\”:{\ “网址\”:[]}},\ “受保护\” :假的,\ “FOLLOWERS_COUNT \”:292,\ “FRIENDS_COUNT \” 196 \ “listed_count \”:4,\ “created_at \”:\“孙 4月22日15:20:46 +0000 2012 \ “\ ”favourites_count \“:1151,\ ”utc_offset \“: - 10800,\ ”的time_zone \“:\” 大西洋 时间 (加拿大)\”,\ “geo_enabled \”:真实的,\ “验证\”:虚假,\ “statuses_count \”:1305,\ “郎\”:\ “恩\”,\ “contributors_enabled \”:假的, \ “is_translator \”:假,\ “is_translation_enabled \”:假,\ “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 \”:假的,\ “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 \”,\“PR ofile_use_background_image \ “:虚假,\” has_extended_profile \ “:真实的,\” DEFAULT_PROFILE \ “:虚假,\” default_profile_image \ “:虚假,\” 下面的\ “:虚假,\” follow_request_sent \ “:虚假,\” 通知\ “:假}”)
我使用自己的Twitter帐户登录,但未显示电子邮件。我知道我有一个与我的Twitter帐户相关的电子邮件。有什么想法吗?
答案 0 :(得分:0)
我在Twitter上遇到了同样的问题。首先将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。 Firebase控制台中的API密钥和API密钥来自dev.twitter。