Wednesday, 4 January 2017

Intigration with Facebook Using java








Intigration with Facebook Using java


First we need register our app in

And in settings put domains as
https://sssss.com

And there we can get the appid and Secret Key

APPID
XXXXXXXXXXXXXX

Secret key
XXXXXXXXXXXXXXXX


And after settings all these go to


And login with the credentials

After that click on the down arrow on get token, after that click on the checkbox publish_actions and click on get token after that token will generate. Copy that token and paste the token in the program.

Run the program.

Post will be posted on the user who has been logged in.


Maven dependency

<dependency>
<groupId>org.scribe</groupId>
<artifactId>scribe</artifactId>
<version>1.3.2</version>
</dependency>


JAVA Code

package com.restfb.example;


import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.FacebookApi;
import org.scribe.model.*;
import org.scribe.oauth.OAuthService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class SharePostOnFacebookWallTimeline {

private static String FB_APP_KEY = "APPID HERE";
private static String FB_APP_SECRET = "APP SECRET HERE";

private static OAuthService getOAuthService()
{
return new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey(FB_APP_KEY)
.apiSecret(FB_APP_SECRET)
.callback("https://yoursite.net/")
.build();
}



private static void postOnFacebookWallTimeline (OAuthService oAuthService, Token accessToken)
{
OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.facebook.com/me/feed");
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
String message = "Welocme to Techwave here we are Having 5 oepenings on JAVA - " + sdf.format(new Date());
request.addBodyParameter("message", message);
String link = "https://yoursite.net";
request.addBodyParameter("link", link);
oAuthService.signRequest(accessToken, request);

Response response = request.send();
System.out.println(response.getCode());
String responseBody = response.getBody();
System.out.println(responseBody);
}

public static void main(String[] args)
{
OAuthService oAuthService = getOAuthService();

Token accessToken, accessToken1;

//facebook app's short-lived access token
//accessToken = getFbAppShortLivedAccessToken(oAuthService);
//accessToken = new Token("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef", "");

//facebook app's long-lived access token
//accessToken1 = getFbAppLongLivedAccessToken(oAuthService, accessToken);
accessToken = new Token("Paste accesstoken here", "");

//postOnFacebookWallTimeline(oAuthService, accessToken1);
postOnFacebookWallTimeline(oAuthService, accessToken);

}

}







No comments:

Post a Comment