[英]How to fix this problem to display email, birthday friends from facebook login?
我正在嘗試登錄 facebook。 但我無法顯示電子郵件,朋友,生日。 我丟了
一些東西,但無法找到問題。 我認為圍繞“請求”,可能是錯誤的。 請幫忙 。 這是輸出>這是代碼:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fb_login)
callbackManager = CallbackManager.Factory.create()
email = findViewById(R.id.txtEmail)
birthday = findViewById(R.id.txtBirthday)
friends = findViewById(R.id.txtFriends)
profile = findViewById(R.id.imgProfile)
loginButton?.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday","user_friends"))
loginButton?.registerCallback(callbackManager, object : FacebookCallback<LoginResult>{
override fun onSuccess(loginResutl: LoginResult?) {
mDialog = ProgressDialog(this@FbLoginActivity)
mDialog!!.setMessage("Retrieving Data")
mDialog!!.show()
request = GraphRequest.newMeRequest(loginResutl!!.accessToken, object : GraphRequest.GraphJSONObjectCallback {
override fun onCompleted(`object`: JSONObject?, response: GraphResponse?) {
mDialog!!.dismiss()
getData(`object`!!)
}
})
//Request graph APi
var parameters : Bundle ?= null
parameters!!.putString("fields", "id,email,birthday,friends")
request!!.setParameters(parameters)
request!!.executeAsync()
}
override fun onCancel() {
}
override fun onError(error: FacebookException?) {
}
})
}
private fun getData(`object`: JSONObject) {
var profile_picture = URL("https://graph.facebook.com/"+`object`.getString("id")+"/picture?width=250&height=250")
Picasso.get().load(profile_picture.toString()).into(profile)
email!!.setText(`object`.getString("email"))
birthday?.setText(`object`.getString("birthday"))
friends?.setText("Friends :" + `object`.getJSONObject("friends").getJSONObject("summary").getString("total_count"))
}
Xml 文件:它顯示 facebook 登錄的 ui 詳細信息。 在這個文件中有 facebook 的登錄按鈕。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp"
tools:context=".FbLoginActivity">
<ImageView
android:id="@+id/imgProfile"
android:layout_width="300dp"
android:layout_height="300dp"
/>
<TextView
android:id="@+id/txtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Email"
/>
<TextView
android:id="@+id/txtBirthday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Birthday"
/>
<TextView
android:id="@+id/txtFriends"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Friends"
/>
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
如果用戶通過電話號碼登錄,則電子郵件可能為空訪問http://stackoverflow.com/questions/29517667/android-facebook-sdk-4-x-how-to-get-email-address-and-facebook -access-token-to和http://stackoverflow.com/questions/29493486/create-a-request-in-facebook-sdk-4-android
Facebook 默認不給用戶生日。您必須使用擴展權限並將您的應用提交給 Facebook 進行審核才能訪問用戶的生日。
看這里:
https://developers.facebook.com/docs/android/graph
Facebook 假定您的電子郵件無效。 要測試它,請使用 Graph API Explorer 並嘗試獲取它。 如果即使在那里您也無法收到您的電子郵件,請在您的個人資料設置中更改它,然后重試。
https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Did%2Cname%2Cemail&
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.