我在android中使用登录facebook。但我没有获得访问令牌。 我收到用户详细信息,如电子邮件,姓名等。 有什么可以告诉我为什么我没有得到令牌?
我正在使用facebook sdk 4.0.0。
MainActivity
var zoomElement = $('#zoom_01');
// destroy old one
zoomElement.removeData('zoom-image');
$('.zoomContainer').remove();
// set new one
zoomElement.attr('data-zoom-image', image_large_size);
zoomElement.attr('src', image_normal_size);
// reinitial elevatezoom
zoomElement.elevateZoom();
Logcat
public class MainActivity extends Activity {
private CallbackManager callbackManager;
private LoginButton loginButton;
private TextView btnLogin;
private ProgressDialog progressDialog;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
String token = loginResult.getAccessToken().getToken();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
};
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this);
callbackManager = CallbackManager.Factory.create();
accessTokenTracker= new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
displayMessage(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_main);
assert getActionBar() != null;
getActionBar().hide();
if(PrefUtils.getCurrentUser(MainActivity.this) != null){
//Intent homeIntent = new Intent(MainActivity.this, LogoutActivity.class);
Intent homeIntent = new Intent(MainActivity.this, ListViewActivity.class);
startActivity(homeIntent);
finish();
}
/*try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.comida",
PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (PackageManager.NameNotFoundException e) {
} catch (NoSuchAlgorithmException e) {
}*/
/* FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
}
private void displayMessage(Profile profile){
if(profile != null){
//textView.setText(profile.getName());
}
}
@Override
protected void onResume() {
super.onResume();
callbackManager=CallbackManager.Factory.create();
loginButton= (LoginButton)findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile", "email","user_friends");
btnLogin= (TextView) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
loginButton.performClick();
loginButton.setPressed(true);
loginButton.invalidate();
loginButton.registerCallback(callbackManager, mCallBack);
loginButton.setPressed(false);
loginButton.invalidate();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
progressDialog.dismiss();
// App code
GraphRequest request = GraphRequest.newMeRequest(
//loginResult.getAccessToken(),
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.e("response: ", response + "");
try {
user = new User();
user.facebookID = object.getString("id").toString();
user.email = object.getString("email").toString();
user.name = object.getString("name").toString();
user.gender = object.getString("gender").toString();
PrefUtils.setCurrentUser(user,MainActivity.this);
}catch (Exception e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"welcome "+user.name,Toast.LENGTH_LONG).show();
Intent intent=new Intent(MainActivity.this,ListViewActivity.class);
//Intent intent=new Intent(MainActivity.this,LogoutActivity.class);
startActivity(intent);
finish();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
progressDialog.dismiss();
}
@Override
public void onError(FacebookException e) {
progressDialog.dismiss();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
/*@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}*/
}
答案 0 :(得分:0)
您可以使用
获取令牌AccessToken.getCurrentAccessToken().getToken();
使用此功能,您可以随时获取令牌,如果您使用LoginResult,则只需在登录时获取一次,当您重新启动应用程序时,不会调用onSuccess。
如果您在重新启动后需要配置文件,请使用
Profile.getCurrentProfile();
如果返回null,则不会显示
答案 1 :(得分:0)
您拥有访问令牌,但您从不直接使用它。
如果您想要通过令牌记录用户的ID,您可以:
Log.w("accessToken", accessToken.getUserId().toString());
正如@Cbroe在评论中提到的那样:
如果您要获取用户详细信息,则必须拥有访问令牌 - 否则API不会在第一时间给你那些。