muthu
muthu

Reputation: 5461

Facebook4j with OAUTH Access Token

Is any one help me to provide a example for OAuth access token for the facebook by getting the user credentials and and allows the fields similar to linkedin API.

I tried to use like

Configuration configuration =  createConfiguration();
        FacebookFactory facebookFactory = new FacebookFactory(configuration );
        Facebook facebookClient = facebookFactory.getInstance();
        AccessToken accessToken = null;
        try{
            OAuthSupport oAuthSupport = new OAuthAuthorization(configuration );
            accessToken = oAuthSupport.getOAuthAppAccessToken();

        }catch (FacebookException e) {
            logger.error("Error while creating access token " + e.getLocalizedMessage(), e);
        }

public Configuration createConfiguration() {
        ConfigurationBuilder confBuilder = new ConfigurationBuilder();

        confBuilder.setDebugEnabled(APIConfiguration.DEBUG_ENABLED);
        confBuilder.setOAuthAppId(APIConfiguration.APP_ID);
        confBuilder.setOAuthAppSecret(APIConfiguration.APP_SECRET);
        confBuilder.setUseSSL(APIConfiguration.USE_SSL);
        confBuilder.setJSONStoreEnabled(APIConfiguration.JSON_STORE_ENABLED);


        Configuration configuration = confBuilder.build();
        return configuration;
    }

i got access token but i could search users it shows

SEVERE: Error while getting the facebook users {"error":{"message":"(#200) Must have a valid access_token to access this endpoint","type":"OAuthException","code":200}}

FacebookException [statusCode=403, response=HttpResponse{statusCode=403, responseAsString='{"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@1232784a, streamConsumed=true}, errorType=OAuthException, errorMessage=(#200) Must have a valid access_token to access this endpoint, errorCode=200]
    at facebook4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:189)
    at facebook4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:65)
    at facebook4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:93)
    at facebook4j.FacebookImpl.get(FacebookImpl.java:2095)
    at facebook4j.FacebookImpl.searchUsers(FacebookImpl.java:1799)
    at facebook4j.FacebookImpl.searchUsers(FacebookImpl.java:1795)

How would i get the OAUTH using call back url i tried redirect_uri with http://www.google.com but it does not give any code.

It is my java console application

Upvotes: 2

Views: 9252

Answers (3)

Bechyňák Petr
Bechyňák Petr

Reputation: 795

Define accessToken as following example:

accessToken = new AccessToken("CAACEdEose0cBAFpfeSxd3WFzkUfm4l4PTKtLblS0hpbOFQcanzYciYMCSuFNOgiZBEtjxWZCHvwU0iP4cTe7aHXeNB5nQOC88ECE1lzVvjNKPjXNsGmJfbNfGEULQ0zEfeTla3Puknj6AGcsPy5VKKEQdJ3FbJ20RRemtgAGh05kgXsXnLrdfCPq6e6eFyu8dWxL1ZBv0EZBe9le3m0U");

where the accesToken string is obtained from https://developers.facebook.com/tools/accesstoken/ enter image description here

Upvotes: 2

Preben Hafnor
Preben Hafnor

Reputation: 170

I think you're missing this line:

facebookClient.setOAuthAccessToken( accessToken );

Upvotes: 0

Michal Toman
Michal Toman

Reputation: 1

I inspired by using your code and I had similar mistake. The solution seems to be to register the application properly in the faceboook developer center = https://developers.facebook.com/ where the application must be created, edited, submitted and approved by the center.

Upvotes: 0

Related Questions