如何在android中获取facebook用户访问令牌? [英] How to get facebook user access token in android?
问题描述
我使用 restfb API 以Java访问我朋友的照片。并且要访问这些照片,我使用 Graph API资源管理器手动生成访问代码,并将其作为参数到restfb API调用。
I am using restfb API to access my friend's photos in Java. And to access these photos, I generate access code manually using Graph API explorer and pass this as a parameter to the restfb API call.
但现在我想通过代码(以编程方式)生成此访问令牌。我看过fb android的样例,特别是hackbook。我没有看到生成的任何访问代码,我可以用于我自己的应用程序。我需要创建一个新的应用程序并获得一些秘密吗?任何建议将不胜感激。
But now I want to generate this access token through code (programmatically). I have seen fb android samples, particularly hackbook. I don't see any access code being generated which I can use for my own application. Do I need to create a new app and get some secret etc? Any suggestion will be appreciated.
我看过这些解决方案( solution-1 & 解决方案-2 )在stackoverflow,但是我还没有从哪里开始?
I have seen these solutions (solution-1 & solution-2) on stackoverflow but I am still not getting where to start?
Update-1 ::
我正在使用以下代码登录并获取登录用户的访问令牌。但问题是它只适用于我在Facebook上创建一个应用程序以生成app_id的帐户。它没有得到其他帐户的回叫。任何建议请。
I am using following code to login and getting access token for logged in user. But the problem is that it only works for the account with which I had created an app on facebook to generate an app_id. It does not get a call back for other accounts. Any suggestion please.
如果您不知道从哪里开始,请按照第6步从头开始创建一个新的应用程序。
And just in case if you don't know where to start, follow step-6 to create a new app from scratch.
public class MainActivity extends Activity implements OnClickListener {
Button login;
TextView accessToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
accessToken = (TextView) findViewById(R.id.accessToken);
login.setOnClickListener(this);
// start Facebook Login
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@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 onResume()
{
Session session = Session.getActiveSession();
if(session != null)
if (session.isOpened()) {
//Toast.makeText(this, session.getAccessToken(), Toast.LENGTH_LONG).show();
accessToken = (TextView) findViewById(R.id.accessToken);
accessToken.setText(session.getAccessToken());
System.out.println("----------------------" + session.getAccessToken() + "---------------------");
}
super.onResume();
}
@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() + "!");
}
}
});
}
}
});
}
}
推荐答案
原始问题的回答已经输入为update-1。但它只适用于管理员用户。
Answer to the original question has already been entered as update-1. But it was only working for admin users.
实际上,在应用创建过程中,我错误地选择了沙盒模式,限制了应用只能访问应用配置页面中添加的开发者的访问权限。因此,禁用此模式后,我也可以为其他用户生成访问令牌。
Actually during app creation, I had wrongly selected 'sandboxed' mode which restricts app's access only to developers added in app configuration page. So after disabling this mode, I was able to generate access token for other users too.
这篇关于如何在android中获取facebook用户访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!