我正在尝试使用facebook login
从我的应用程序登录,但我在jsonObject
回复的内容只是id和name,而我请求loginButton.setReadPermissions("public_profile")
时没有其他内容。任何人都可以找出问题所在?这是我的代码。
public class LoginActivity extends AppCompatActivity {
LoginButton loginButton;
CallbackManager callbackManager;
String id, name, gender, ageRange;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
loginButton=(LoginButton)findViewById(R.id.login_button);
callbackManager= CallbackManager.Factory.create();
loginButton.setReadPermissions("public_profile");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken=loginResult.getAccessToken();
GraphRequest graphRequest= GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
if (jsonObject!=null)
{
Log.e("JSON Object",jsonObject.toString());
Log.e("GraphResponse",graphResponse.toString());
try
{
id=jsonObject.getString("id");
name=jsonObject.getString("name");
gender=jsonObject.getString("gender");
ageRange=jsonObject.getString("age_range");
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
});
graphRequest.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(),"Cancelled", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getApplicationContext(),"Error while Connecting", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}
答案 0 :(得分:10)
使用图形API,请使用以下代码。
获取所有值,只需传递参数
parameters.putString(&#34;字段&#34;,&#34; id,name,link,email,first_name,last_name,gender&#34;);
=============================
完整代码
public static CallbackManager callbackManager;
Button btn_login;
<强> OnCreate中强>
FacebookSdk.sdkInitialize(getApplicationContext());
btn_login = (Button) findViewById(R.id.btn_facebook);
btn_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
callbackManager = CallbackManager.Factory.create();
// loginButton.setReadPermissions(Arrays.asList("user_status"));
// Set permissions
LoginManager.getInstance().logInWithReadPermissions(
LoginActivity.this,
Arrays.asList("email", "public_profile"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("Success");
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject json,
GraphResponse response) {
if (response.getError() != null) {
// handle error
System.out
.println("ERROR");
} else {
System.out
.println("Success");
try {
String jsonresult = String
.valueOf(json);
System.out
.println("JSON Result"
+ jsonresult);
String str_email = json
.getString("email");
String str_id = json
.getString("id");
String str_firstname = json
.getString("first_name");
String str_lastname = json
.getString("last_name");
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,first_name,last_name,gender");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.d("TAG_CANCEL", "On cancel");
}
@Override
public void onError(FacebookException error) {
Log.d("TAG_ERROR", error.toString());
}
});
}
});
<强> onActivityResult 强>
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}
<强>清单强>
<!-- facebook -->
<activity
android:name="com.facebook.FacebookActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />
<!-- facebook finish -->
参考链接:https://developers.facebook.com/docs/android/graph
感谢。
答案 1 :(得分:2)
$http.post(scope.serviceBase + 'api/Property/PostAsync', data,
{ transformRequest: angular.identity, headers: {'Content-Type':undefined} })