Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get facebook user access token in android?

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() + "!");
                                    }
                                }
                            });
                }
            }
        });

    }

    }
like image 494
Junaid Avatar asked May 02 '13 04:05

Junaid


People also ask

What is Facebook app token?

An access token is an opaque string that identifies a user, app, or Page and can be used by the app to make graph API calls. When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.

How do I get a short live access token on Facebook?

Go to https://developers.facebook.com/tools/explorer/ and select your app from the first drop down menu, in the left. Click on the button "Get access token", and in the "Select Permissions" window, click in "Extended Permissions" and check manage_pages and publish_stream, and click in "Get Access Token" blue button.


1 Answers

Using the Facebook SDK the better way to manage the authentication of the user is by means of the Session class. When you have a valid instance of the Session class you just have to call the getAccessToken() on it in order to obtain the String representing the access token.

Session session = Session.getActiveSession();
if (session != null && session.getState().isOpened()){
     Log.i("sessionToken", session.getAccessToken());
     Log.i("sessionTokenDueDate", session.getExpirationDate().toLocaleString());
}
like image 77
5agado Avatar answered Oct 23 '22 07:10

5agado