我使用 restfb API 访问我的朋友的照片在Java中。而要获得这些照片,我生成访问code手动使用图形API浏览器,并通过这个作为参数传递给restfb API调用。
但现在我要生成到code(编程)这个访问令牌。我所看到的FB机器人样品,特别hackbook。我没有看到任何访问code所产生的,我可以用我自己的应用程序。我是否需要创建一个新的应用程序,并得到一些秘密等?任何建议将AP preciated。
我已经看到了这些解决方案(solution-1 &安培; solution-2)对计算器,但我仍然没有得到哪里开始呢?
更新-1 ::
我使用下面的code登录并获得访问令牌登录的用户。但问题是,它仅适用于与我创造了在Facebook上的应用程序生成APP_ID的帐户。它没有得到回电其他账户。任何建议,请。
和公正的情况下,如果你不知道从哪里开始,遵循的第六步从头开始创建一个新的应用程序。
公共类MainActivity扩展活动实现OnClickListener {
按钮的登录;
TextView的accessToken;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
登录=(按钮)findViewById(R.id.login);
accessToken =(TextView中)findViewById(R.id.accessToken);
login.setOnClickListener(本);
//开始的Facebook登录
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
super.onActivityResult(要求code,因此code,数据);
Session.getActiveSession()onActivityResult(这一点,要求code,因此code,数据)。
}
@覆盖
公共无效onResume()
{
会话会话= Session.getActiveSession();
如果(会话!= NULL)
如果(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();
}
@覆盖
公共无效的onClick(视图v){
//开始的Facebook登录
Session.openActiveSession(这一点,真,新Session.StatusCallback(){
//回调时,会话状态变化
@覆盖
公共无效呼叫(会话的会话,SessionState会的状态,
例外的例外){
如果(session.isOpened()){
//使请求/ ME API
Request.executeMeRequestAsync(会话,
新Request.GraphUserCallback(){
//后与用户图形API响应回调
// 目的
@覆盖
公共无效onCompleted(GraphUser用户,
响应响应){
如果(用户!= NULL){
TextView的欢迎=(TextView中)findViewById(R.id.welcome);
welcome.setText(你好
+ user.getName()+)!;
}
}
});
}
}
});
}
}
解决方案
答案原来的问题已经进入了作为更新-1。但它只是工作的管理员用户。
其实应用程序创建过程中,我错误地选择了沙箱模式,只限制给开发商添加应用程序配置页面的应用程序的访问。因此禁用这一模式后,我就能够产生其他用户的访问令牌了。
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.
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.
I have seen these solutions (solution-1 & solution-2) on stackoverflow but I am still not getting where to start?
Update-1::
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.
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() + "!");
}
}
});
}
}
});
}
}
解决方案
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.