我一直通过Facebook最新的SDK做一些登录项目,即3.0。 我得到在获取用户访问令牌一个困难时期。 我已经在互联网上搜索和所有的,最大的成果是使用旧的SDK。 下面是一些代码位,我已经拍摄Facebook的Android SDK中教程 :
public class LoginActivity extends Activity implements OnClickListener {
Button login;
TextView accessToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login = (Button) findViewById(R.id.login);
accessToken = (TextView) findViewById(R.id.accessToken);
login.setOnClickListener(this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
@Override
public void onClick(View v) {
// start Facebook Login
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
// callback after Graph API response with user
// object
@Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.welcome);
welcome.setText("Hello "
+ user.getName() + "!");
}
}
});
}
}
});
}
}
登录成功,我可以在应用中看到用户名,由Facbeook教程的建议。
我曾尝试老方法,但所有这些现在过时。 请指导我,让用户访问令牌。 帮助将不胜感激。
谢谢。
Answer 1:
在该方法的onResume()添加以下代码(在这种情况下,我使用Toast.makeText看登录后的接入令牌):
Session session = Session.getActiveSession();
if (session.isOpened()) {
Toast.makeText(getActivity(), session.getAccessToken(), Toast.LENGTH_LONG).show();
}
我用getActivity,因为它是在我的情况下片段,如果你在活动中有你的登录按钮使用的“本”,而不是“getActivity()”
Answer 2:
注:会话类是在新的Facebook SDK中删除
之后Facebook SDK version 4.X
,你应该使用下列内容:
AccessToken token = AccessToken.getCurrentAccessToken();
if (token != null) {
Toast.makeText(getActivity(), token, Toast.LENGTH_LONG).show();
}
更改日志引用在这里。
从SDK 3.X迁移参考到4.x
Answer 3:
我在Android的是新..
我用上面的代码只得到令牌。
对于Facebook的SDK 4.21.0
编译“com.facebook.android:facebook-android-sdk:4.21.0”
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
AccessToken token = AccessToken.getCurrentAccessToken();
Log.d("access only Token is", String.valueOf(token.getToken()));
String facebook_id_token = String.valueOf(token.getToken());
Answer 4:
要获得的accessToken对于Facebook-SDK 4 *或以上。 添加facebookSDKInitialize此后线()
FacebookSdk.setIsDebugEnabled(true);
FacebookSdk.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKEN);
之后,使用电码的这些线,
AccessToken token = AccessToken.getCurrentAccessToken();
Log.d("Access Token is",token);
样品:
GraphRequestAsyncTask graphRequestAsyncTask = new GraphRequest(
login_result.getAccessToken(),
//AccessToken.getCurrentAccessToken(),
"/me/friends",
bundle,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONArray rawName = response.getJSONObject().getJSONArray("data");
Log.d("rawName friendList",String.valueOf(rawName));
AccessToken token = AccessToken.getCurrentAccessToken();
Log.d("access token is: ",String.valueOf(token));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
).executeAsync();
Answer 5:
下面的代码更新:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
/** This block obtains Facebook UserID and Token */
AccessToken token = AccessToken.getCurrentAccessToken();
if (token != null) {
Toast.makeText(this, token.toString(), Toast.LENGTH_SHORT).show();
Log.e(TAG, "Token: " + token.getToken());
Log.e(TAG, "UserID: " + token.getUserId());
}
/***/
}
Answer 6:
我成功地设法Facebook的SDK中我我用下面的代码帮助整合:
public class MainActivity extends Activity implements OnClickListener {
Facebook fb;
Button button;
SharedPreferences sp;
//TextView welcome;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String APP_ID = getString(R.string.APP_ID);
fb=new Facebook(APP_ID);
sp=getPreferences(MODE_PRIVATE);
String access_token=sp.getString("access token",null);
long expires=sp.getLong("access expires",0);
if(access_token!=null){
fb.setAccessToken(access_token);
}
if(expires!=0){
fb.setAccessExpires(expires);
}
button=(Button)findViewById(R.id.login);
//pic=(ImageView)findViewById(R.id.picture_pic);
button.setOnClickListener(this);
updateButtonImage();
}
private void updateButtonImage() {
// TODO Auto-generated method stub
if(fb.isSessionValid()){
try{
//post.setVisibility(Button.VISIBLE);
button.setBackgroundResource(R.drawable.logout_button);
/*pic.setVisibility(ImageView.VISIBLE);
JSONObject obj=null;
URL img_url = null;
String jsonuser=fb.request("me");
obj=Util.parseJson(jsonuser);
String id = obj.optString("id");
String name = obj.optString("name");
welcome.setText("Welcome "+name);
img_url=new URL("http://graph.facebook.com/"+id+"/picture?type=large");
Bitmap bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());
pic.setImageBitmap(bmp);*/
buttonClicks();
}catch(Exception e){
e.printStackTrace();
}
}else{
//post.setVisibility(Button.INVISIBLE);
button.setBackgroundResource(R.drawable.login_button);
//pic.setVisibility(ImageView.INVISIBLE);
}
}
public void buttonClicks()
{
Bundle params = new Bundle();
params.putString("image", "your string");
fb.dialog(MainActivity.this, "feed", params, new DialogListener(){
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
}
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(fb.isSessionValid()){
//button close our session - log out facebook
try {
fb.logout(getApplicationContext());
updateButtonImage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
//login to fb
fb.authorize(MainActivity.this, new String[] {"email"}, new DialogListener(){
@Override
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor editor=sp.edit();
editor.putString("access_token", fb.getAccessToken());
editor.putLong("access expires", fb.getAccessExpires());
editor.commit();
updateButtonImage();
}
@Override
public void onFacebookError(FacebookError e) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "onFacebookError", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(DialogError e) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "onCancel", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
fb.authorizeCallback(requestCode, resultCode, data);
}
}
我希望,你会得到一些帮助与此有关。
文章来源: Android: Facebook get “User Access Token”, on successful login