User Access Token

5,456 views
Skip to first unread message

Michael Christensen

unread,
Jul 15, 2013, 7:24:47 PM7/15/13
to faceb...@googlegroups.com
I have a fledgling Facebook app with one user currently having given it permissions to access their data. However, in my Java code, when I try to get data about that user that I know requires a user access token, the error response I get is:

FacebookException [statusCode=400, response=HttpResponse{statusCode=400, responseAsString='{"error":{"message":"A user access token is required to request this resource.","type":"OAuthException","code":102}}

I am able to set up the App Access Token properly and query facebook for public data, but how do I go about getting a User Access Token for users who have installed my app so I can access the data which they gave me permissions to access when they agreed to using my app?

Thank you,
Michael

Ryuji Yamashita

unread,
Jul 17, 2013, 12:57:22 AM7/17/13
to Michael Christensen, faceb...@googlegroups.com
Could you paste your code?



--
Twitter: @facebook4j https://twitter.com/facebook4j
---
You received this message because you are subscribed to the Google Groups "Facebook4J" group.
To unsubscribe from this group and stop receiving emails from it, send an email to facebook4j+...@googlegroups.com.
To post to this group, send email to faceb...@googlegroups.com.
Visit this group at http://groups.google.com/group/facebook4j.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Christensen

unread,
Jul 17, 2013, 6:45:00 PM7/17/13
to faceb...@googlegroups.com, Michael Christensen
Here is my code (a test I'm doing to figure this user access token business out). A properties file elsewhere contains my appSecret and appId:

package facebook;

import static org.junit.Assert.*;
import org.junit.Test;
import facebook4j.Facebook;
import facebook4j.FacebookBase;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.Friend;
import facebook4j.Post;
import facebook4j.ResponseList;
import facebook4j.User;
import facebook4j.auth.AccessToken;
import facebook4j.auth.Authorization;
import facebook4j.auth.AuthorizationFactory;

public class FacebookAuthenticationTest {

    private static final boolean OVERLY_VERBOSE = true;

    @Test
    public void test() throws FacebookException {
        Facebook fb = new FacebookFactory().getInstance();
        // App access token
        AccessToken oAuthAppAccessToken = fb.getOAuthAppAccessToken();
        fb.setOAuthAccessToken(oAuthAppAccessToken);
   
        User user3 = fb.getUser("100005941062130");
        assertEquals("Darth Vader Dataminor", user3.getName());
       
        // User access token
        String dVaderTokenString =
            "CAAEtY3a6duUBAKYBrZCDhZA62O5w6SGD8myHtRPdnnDADmjGBTbmX9ItRPE6YDjExqG86FZBvIE8Bmta9R9ZA3aZCbZAv1cJAtjPBpd78X3gA51ZAR66KEuqvXzqQERx2ZCMpjXGHgnVi9YhelCe3RPSSzZB2pDfG0LeY1bTk558CggZDZD";
        AccessToken a = new AccessToken(dVaderTokenString);
        fb.setOAuthAccessToken(a);


        ResponseList<Friend> friends = fb.getFriends(user3.getId());
        assertTrue(friends != null);
        if (OVERLY_VERBOSE) {
            System.out.println("Number of friends:" + friends.size());
            for (Friend f : friends) {
                System.out.println(f.getName());
                ResponseList<Post> statuses = fb.getStatuses(f.getId());
                for (Post p: statuses) {
                    System.out.println("\t" + p.getMessage());
                }
            }
        }
    }
}


So user3 is a test user of the application. If I omit the bold lines, the error that I mentioned in the previous post is given where the code highlighted in red occurs, since statuses require a user access token. However, when I manually go into my application's Settings/Developer Roles and select Show Token of this user to copy and paste that string here into my code in the bold lines, I am able to access the data that I should be able to, given the permissions the user granted, and this test can complete normally.

My question is what have people done to be able to get this user access token programatically? Does it involve calling into the API somehow, does facebook4j have such a way to do this, or can I only get a user access token when the user is using the app and interacting with my website's PHP/Javascript, so that instead the PHP has to call into the java code and pass in the user access token it gets after finishing user authentication?

Thank you,
Michael

Ryuji Yamashita

unread,
Jul 17, 2013, 9:56:35 PM7/17/13
to Michael Christensen, faceb...@googlegroups.com
Hi,

If your application is not in Java, I think that you must propagate the user access token to Facebook4J, or propagate the 'code' that is included in Login dialog response to Facebook4J.
If your application is in Java, you can through the Login Flow like this:

If you need only to go well only this test case,
you just add this to your facebook4j.properties
    oauth.accessToken=CAAEtY3a6du...558CggZDZD
and delete the following
>> // App access token
>> AccessToken oAuthAppAccessToken = fb.getOAuthAppAccessToken();
>> fb.setOAuthAccessToken(oAuthAppAccessToken);

Best,

Kumar Surya Mani

unread,
Jul 27, 2013, 2:10:03 PM7/27/13
to faceb...@googlegroups.com
Hi Michael,

Sorry for interrupting your discussion thread.Would you please share; how have you setup the App Access Token and query Facebook for public data; I am able to generate the access token using the url 'https://graph.facebook.com/oauth/access_token?client_id=XXXXX&client_secret=XXXXXXXXXX&grant_type=client_credentials' however if I pass the same access token to the ConfigBuilder it is throwing exception as mentioned below: 

FacebookException [statusCode=403, response=HttpResponse{statusCode=403, respons
eAsString='{"error":{"message":"(#200) Must have a valid access_token to access 
this endpoint","type":"OAuthException","code":200}}
', is=sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@6e0031c0, stre
amConsumed=true}, errorType=OAuthException, errorMessage=(#200) Must have a vali
d access_token to access this endpoint, errorCode=200]

The code snippet for my test application is mentioned below:

try {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
 .setOAuthAppId("XXXXXXX")
 .setOAuthAppSecret("XXXXXXXXXXXXXXXXXX")
 .setOAuthAccessToken("XXXXXXXXXXXXXXX");  //this access token I have generated using the URL mentioned above.
FacebookFactory ff = new FacebookFactory(cb.build());
Facebook facebook = ff.getInstance();
ResponseList<User> results = facebook.searchUsers("Sachin Tendulkar");
System.out.println(results.getCount());
} catch (FacebookException e) {
e.printStackTrace();

The line marked in red is the failing point.

Kumar Surya Mani

unread,
Jul 27, 2013, 2:45:29 PM7/27/13
to faceb...@googlegroups.com

If I call facebook.getPosts()/getActivities() instead of code marked red below, I get the exception as following:

FacebookException [statusCode=400, response=HttpResponse{statusCode=400, respons
eAsString='{"error":{"message":"An active access token must be used to query inf
ormation about the current user.","type":"OAuthException","code":2500}}
', is=sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@729084d2, stre
amConsumed=true}, errorType=OAuthException, errorMessage=An active access token 
must be used to query information about the current user., errorCode=2500]


Please share how to set up the App Access Token properly and query facebook for public data.

thanks & regards,
surya

Ryuji Yamashita

unread,
Jul 29, 2013, 3:24:41 AM7/29/13
to Kumar Surya Mani, faceb...@googlegroups.com
Hi Surya,

Search the users requires "user" access token, not app access token.
"All other endpoints require a user access token." in Access Tokens section.

Therefore, you need to get a user access token at Graph API Explorer etc.

Best,


--

Kumar Surya Mani

unread,
Jul 29, 2013, 4:01:52 AM7/29/13
to Ryuji Yamashita, faceb...@googlegroups.com
Thanks Ryuji for responding.

regardss,
surya
--
Warms n Regards,
Kumar Surya Mani

      **They conquer who believe they can. - John Dryden

Rahul Gundu

unread,
Nov 10, 2014, 6:47:19 AM11/10/14
to faceb...@googlegroups.com, mike...@gmail.com
hi mike,

In your code the user access token is hard coded. Is there any to get that key form the code.

Đông Anh

unread,
Jun 8, 2015, 3:02:14 PM6/8/15
to faceb...@googlegroups.com, Xuân Hiệp Nguyễn, ksury...@gmail.com
Dear Ryuji Yamashita 

Can you help me out this problem. I created new facebook app and login to my website (account 1). I use userAccessToken to postStatus or postFeed Its OK, but If i use 
another facebook account( account 2),I cant access post..() method.

Here is error. statusCode=403, errorType='OAuthException', errorMessage='(#200) The user hasn't authorized the application to perform this action'.

I have followed on your login example to get access token https://github.com/roundrop/facebook4j-oauth-example. It works OK with account 1, but account 2 always has error. Thanks in advance
Reply all
Reply to author
Forward
0 new messages