Vaadin with Instagram API - java

To use my app, the user must log in to their instagram account and give my app permission to access their information.
Once this is done, a URL with a code attached to the back will be returned (e.g. www.google.com/code=123456789).
The code is the exchanged for an access token.
My question is:
Can my app automatically get the code from the url and use it instead of the user having to copy and paste it in?
Thank you for reading!
Code:
TextField code = new TextField("Enter Code: ");
Button button = new Button("Click here to authenticate");
String callbackUrl = "http://reveal-it.appspot.com/oauthtest";
String clientID = "XXX";
String clientSecret = "XXX";
Token EMPTY_TOKEN = null;
InstagramService service = new
InstagramAuthService().apiKey(clientId)
.apiSecret(clientSecret)
.callback(callbackUrl)
.build();
String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
getUI().getPage().open(authorizationUrl, "_blank");
next();
}
});
public void next(){
String verifierCode = code.getValue();
Verifier verifier = new Verifier(verifierCode);
Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
Instagram instagram = new Instagram(accessToken);
UserInfo userInfo = instagram.getCurrentUserInfo();
}
Step 1: Authenticate User
Step 2: Get URL with code attached at the back that user has to copy and paste into textfield
(This is a test url that prints the code)

These social platformas usually use oauth for login and right granting.
There exists a vaadin addon for this.
https://vaadin.com/directory#addon/oauth-popup-add-on

Related

Posting status message to facebook page

Im trying to post some status message on my facebook page, not my personal facebook profile account, but on page i created separately.
My current code looks like this:
import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.Post;
import facebook4j.ResponseList;
import facebook4j.conf.Configuration;
import facebook4j.conf.ConfigurationBuilder;
public class FacebookImpl {
// from https://developers.facebook.com/apps/
static String appId = "11removed";
// from https://developers.facebook.com/apps/
static String appSecret = "c0removed";
// from https://developers.facebook.com/tools/accesstoken/
static String appToken = "11removed";
// my facebook page
static String myFaceBookPage = "niremoved";
public static void main(String[] args) throws FacebookException {
// Make the configuration builder
ConfigurationBuilder confBuilder = new ConfigurationBuilder();
confBuilder.setDebugEnabled(true);
// Set application id, secret key and access token
confBuilder.setOAuthAppId(appId);
confBuilder.setOAuthAppSecret(appSecret);
confBuilder.setOAuthAccessToken(appToken);
// Set permission
// https://developers.facebook.com/docs/facebook-login/permissions
confBuilder.setOAuthPermissions("manage_pages,publish_pages,publish_actions");
confBuilder.setUseSSL(true);
confBuilder.setJSONStoreEnabled(true);
// Create configuration object
Configuration configuration = confBuilder.build();
// Create FacebookFactory and Facebook instance
FacebookFactory ff = new FacebookFactory(configuration);
Facebook facebook = ff.getInstance();
// this one works fine
System.out.println(getFacebookPostes(facebook, myFaceBookPage));
// try to post status
// FacebookException{statusCode=403, errorType='OAuthException',
// errorMessage='(#200) The user hasn't authorized the application to perform this action', errorCode=200, errorSubcode=-1, version=2.4.5}
facebook.postStatusMessage(myFaceBookPage, "Test Facebook4J.");
}
public static String getFacebookPostes(Facebook facebook, String page) throws FacebookException {
ResponseList<Post> results = facebook.getPosts(page);
// as example just to see if i get any data
return results.get(0).getMessage();
}
}
Problem is that i cant post any message on page using this code: facebook.postStatusMessage(myFaceBookPage, "Test Facebook4J."); but i can get messages already posted (via facebook web interfaces) with method getFacebookPostes.
Can anyone help me with this one ? And please do not paste some random dev.Facebook link to look into API.
What i did:
- create app on https://developers.facebook.com/apps/ i have appid, appsecret
Thanks

YouTube API v3 Not Displaying Exceptions

I just started using YouTube API for Java and I'm having a tough time trying to figure out why things don't work since exception/stack trace is no where to be found. What I'm trying to do is to get list of videos uploaded by current user.
GoogleTokenResponse tokenFromExchange = new GoogleTokenResponse();
tokenFromExchange.setAccessToken(accessToken);
GoogleCredential credential = new GoogleCredential.Builder().setJsonFactory(JSON_FACTORY).setTransport(TRANSPORT).build();
credential.setFromTokenResponse(tokenFromExchange);
YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
channelRequest.setMine(true);
channelRequest.setFields("items/contentDetails,nextPageToken,pageInfo");
ChannelListResponse channelResult = channelRequest.execute();
I don't see anything wrong with this code and also tried removing multiple things, but still not able to get it to work. Please let me know if you have run into a similar issue. The version of client library I'm using is v3-rev110-1.18.0-rc.
YouTube API has some working code and you can use it.
public static YouTubeService service;
public static String USER_FEED = "http://gdata.youtube.com/feeds/api/users/";
public static String CLIENT_ID = "...";
public static String DEVELOPER_KEY = "...";
public static int getVideoCountOf(String uploader) {
try {
service = new YouTubeService(CLIENT_ID, DEVELOPER_KEY);
String uploader = "UCK-H1e0S8jg-8qoqQ5N8jvw"; // sample user
String feedUrl = USER_FEED + uploader + "/uploads";
VideoFeed videoFeed = service.getFeed(new URL(feedUrl), VideoFeed.class);
return videoFeed.getTotalResults();
} catch (Exception ex) {
Logger.getLogger(YouTubeCore.class.getName()).log(Level.SEVERE, null, ex);
}
return 0;
}
This simple give you the number of videos a user has. You can read through videoFeed using printEntireVideoFeed prepared on their api page.

Using kSoap on Android to retrieve data, works on the first execution but return null on subsequent execution

I am working on an Android application which allow user to write feedback and then the articleID, ticketID and ticketnumber of the user will be generated by the server and be returned to the user.
There are two activity in this application. MainActivity allow the user to enter their details and a submit button will start Process activity that will send the details to the server and shows the articleID, ticketID and ticketnumber that are returned.
The problem is, it will only work once every time the application is started. For example, a user open the application and enter his details, the submit button is pressed and the corresponding articleID, ticketID and ticketnumber are returned. Then he tries to submit a second one by returning to the previous activity. He enters his detail again and press submit. This time, null is returned.
Images of example is shown here http://imgur.com/a/uY6gR
However, the application works again if it is quitted and the RAM is cleared.
I tried to use this method here to restart the application but still it did not work.
Below is the kSoap code in the Process activity.
public class Process extends Activity{
private String URL = " /*WORKING URL*/";
private String NAMESPACE = "/*WORKING URL*/";
private String soapUsername = "/*WORKING USERNAME*/";
private String soapPass = "/*WORKING PASSWORD*/";
private String METHOD_NAME = "TicketCreate";
private String SOAP_ACTION = "/*WORKING URL*/";
private Handler handler = new Handler();
private Thread thread;
TextView emailT, subjectT, complaintT, responseT, nameT;
String email, subject, complaint, name;
String articleid , ticketid ,ticketnumber;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.process);
webservice();
nameT = (TextView) findViewById(R.id.name);
emailT = (TextView) findViewById(R.id.email);
subjectT = (TextView) findViewById(R.id.subject);
complaintT = (TextView) findViewById(R.id.complaint);
responseT = (TextView) findViewById(R.id.responsevalue);
Intent i = getIntent();
// Receiving the Data
name = i.getStringExtra("name");
email = i.getStringExtra("email");
subject = i.getStringExtra("subject");
complaint = i.getStringExtra("complaint");
// Displaying Received data
nameT.setText(name);
emailT.setText(email);
subjectT.setText(subject);
complaintT.setText(complaint);
Button fin= (Button)findViewById(R.id.finish);
fin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
public void webservice(){
thread = new Thread(){
public void run(){
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Set all input params
request.addProperty("UserLogin", soapUsername);
request.addProperty("Password", soapPass);
Hashtable<String, String> ticket = new Hashtable<String, String>();
ticket.put("Title", subject);
ticket.put("CustomerUser", email);
ticket.put("CustomerID", "soapwebnologin");
ticket.put("QueueID", "3");
ticket.put("State", "new");
ticket.put("PriorityID", "1");
ticket.put("Lock", "unlock");
ticket.put("OwnerID", "1");
request.addProperty("Ticket", ticket);
Hashtable<String, String> article = new Hashtable<String, String>();
article.put("Subject", subject);
article.put("Body", complaint);
article.put("ContentType", "text/plain; charset=utf8");
request.addProperty("Article", article);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
new MarshalHashtable().register(envelope);
envelope.dotNet = true;
envelope.bodyOut = request;
String check = checkSSL(URL);
if(check == "SSL"){
KeepAliveHttpsTransportSE httpT = new KeepAliveHttpsTransportSE("/*WORKING URL*/", /*WORKING PORT*/, METHOD_NAME, 15000);
httpT.debug = true;
httpT.call(SOAP_ACTION, envelope);
KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
articleid = ks.getProperty(0).toString();
ticketid = ks.getProperty(1).toString();
ticketnumber = ks.getProperty(2).toString();
Log.e("dump Request: " ,httpT.requestDump);
Log.e("dump response: " ,httpT.responseDump);
Log.e("object response : ", ks.toString());
}
else{
HttpTransportSE httpT = new HttpTransportSE(URL);
httpT.debug = true;
httpT.call(SOAP_ACTION, envelope);
KvmSerializable ks = (KvmSerializable)envelope.bodyIn;
articleid = ks.getProperty(0).toString();
ticketid = ks.getProperty(1).toString();
ticketnumber = ks.getProperty(2).toString();
Log.e("dump Request: " ,httpT.requestDump);
Log.e("dump response: " ,httpT.responseDump);
Log.e("object response : ", ks.toString());
}
}
catch(Exception e)
{
e.printStackTrace();
}
handler.post(createUI);
}
};
thread.start();
}
final Runnable createUI = new Runnable() {
public void run(){
responseT.setText("Your ticket id =" + ticketid+ " Article id ="+ articleid+" TICKET NUMBER ="+ ticketnumber);
}
};
protected String checkSSL(String url){
String https = url.substring(0, 4);
if(https == "https"){
return "SSL";
}
else{
return "noSSL";
}
}
}
EDIT: When I rotate the the screen, it requested another ticket from the server and it actually works. I am so confused.
Apparently it is a bug which can be fixed by adding this line:
System.setProperty("http.keepAlive", "false");
I ran into an Android OS bug recently that is pretty harsh related to
HTTPS connections. Basically, what happens is this:
You want to setup a connection between the phone and a server, and you need to control both the input and the output. As a result, you
use URL.openConnection(), with setDoInput() and setDoOutput() set to
true:
URL url = new URL("https://blahblahblah.com"); URLConnection conn =
url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true);
At some point you use both conn.getOutputStream() to write to the
stream, then conn.getInputStream() to get the response.
You're doing an HTTPS connection. Some people report this happening on normal HTTP, but I've only seen it happen on HTTPS.
The first request goes through fine and dandy.
The second time you try to make the request, the connection doesn't send any data out and doesn't receive any data; it looks like it
happens instantly. If you cast to an HttpURLConnection,
conn.getResponseCode() returns -1 instead of anything meaningful.
In other words, every other request, the request fails outright. This
is a noted bug in Android, but it isn't fixed yet in any released
versions. Even when it's fixed, you'll still have to deal with this on
older versions of Android.
There are a few workarounds. The first is to simply not use
URLConnection; if you can find some way around it, avoid it. The
second is to repeatedly make the same request until it works; it's a
little too much of a hack for my tastes, but it'll work.
Then there's the third workaround, which I do not claim to understand
why it fixes the issue but it does. Just set this setting when your
application begins:
System.setProperty("http.keepAlive", "false");
Unfortunately this has some drawbacks (keep-alive is a good thing
normally), but in comparison to mysteriously failed requests I'll
ditch it.
Source: http://daniel-codes.blogspot.com/2010_07_01_archive.html
And regarding the problems with orientation change, read here
Change screen orientation in Android without reloading the activity

Login to twitter and tweet from Java (web-app)

I want to write a simple java (or java web app) program that will allow a user to log in and post a twit. I don't even need a user interface. I can simply hard code the twit, userId, and password. I just want to know the process. I have been looking for a while now, and I have had no success so far. The following code which was finally supposed to work does not work.
The code is a simple application as opposed to a web-app. Does anyone have some code that will work with the present Twitter API? I have been trying to use twitter4j.
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
public class TwitterUtils {
public static void main(String[] args) {
try {
final String consumerKey = "**********";
final String consumerSecret = "**********";
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
RequestToken requestToken = twitter.getOAuthRequestToken();
String token = requestToken.getToken();
String tokenSecret = requestToken.getTokenSecret();
System.out.println("My token :: " + token);
System.out.println("My token Secret :: " + tokenSecret);
//AccessToken a = new AccessToken(token, tokenSecret);
//twitter.setOAuthAccessToken(a);
twitter.updateStatus("If you're reading this on Twitter, it worked!");
} catch (TwitterException e) {
e.printStackTrace();
}
}//main
}//TwitterUtils
With the AccessToken lines commented I get the error
Exception in thread "main" java.lang.IllegalStateException: Authentication credentials are missing. See http://twitter4j.org/configuration.html for the detail. at twitter4j.TwitterBaseImpl.ensureAuthorizationEnabled(TwitterBaseImpl.java:205)
at twitter4j.TwitterImpl.updateStatus(TwitterImpl.java:453)
at playaround.TwitterUtils.main(TwitterUtils.java:55)
Java Result: 1
When I uncomment the lines, the error reads
Exception in thread "main" java.lang.IllegalArgumentException: Invalid access token format. at twitter4j.auth.AccessToken.<init>(AccessToken.java:50)
at playaround.TwitterUtils.main(TwitterUtils.java:53)
Does anyone have a complete solution I may use? Thanks.
Besides consumerKey and consumerSecret (your application's key and secret) you need accessToken from the user that is using your application. You get this accessToken from Twitter using OAuth protocol.
Through this link (
http://module.minic.ro/how-to-make-a-twitter-application-tutorial/ ) you can generate consumer key,consumer secret, access token, and access token secret keys
Use this code when you get keys for Twitter object:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("*********************")
.setOAuthConsumerSecret("******************************************")
.setOAuthAccessToken("**************************************************")
.setOAuthAccessTokenSecret("******************************************");
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
http://twitter4j.org/en/configuration.html

How to connect facebook with java desktop application

I need to connect and authenticate users from java desk top application , i have tried facebook-java-api using facebookjsonclient and facebookRestclient but not able to get session key. is there any changes in facebook due to which we r not able to connect , or is there asny other best java api or example how to connect. my code is
private static void getUserID(String email, String password){
String session = null;
try {
HttpClient http = new HttpClient();
http.getHostConfiguration().setHost("www.facebook.com");
String api_key = "key";
String secret = "sec";
FacebookJaxbRestClient client = new FacebookJaxbRestClient(api_key, secret);
System.out.println("====>"+client.isDesktop());
String token = client.auth_createToken();
System.out.println(" :::::::"+token);
System.out.println(" :::::::::: "+token);
PostMethod post = new PostMethod("/login.php?");
post.addParameter("api_key", api_key);
post.addParameter("email", email);
post.addParameter("pass", password);
int postStatus = http.executeMethod(post);
System.out.println("url : " + post.getURI());
System.out.println("Response : " + postStatus);
for (Header h : post.getResponseHeaders()) {
System.out.println(h);
}
session = client.auth_getSession(token); // Here I am getting error
System.out.println("Session string: " + session);
long userid = client.users_getLoggedInUser();
//System.out.println("User Id is : " + userid);*/
} catch (FacebookException fe) {
fe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
AFAIK , there is no way as of now to connect to facebook from a "desktop app" in a straight forward way. You can use apache http client library to mimick a browser and get it done. But it cannot be guranteed to work always.I have also been trying to do it for sometime with some libraries,but they seem broken.
I've had some success doing this. My approach was to use an embedded browser to display the authentication to the user. Facebook handles the authentication and redirects you to a "login successful" page with the access token and expiration time tacked onto the URL as GET data. Most of the code below is for creating and displaying the browser using the org.eclipse.swt library.
private static final String APP_ID = "###########";
private static final String PERMISSIONS =
"COMMA SEPARATED LIST OF REQUESTED PERMISSIONS";
private String access_token;
private long expirationTimeMillis;
/**
* Implements facebook's authentication flow to obtain an access token. This
* method displays an embedded browser and defers to facebook to obtain the
* user's credentials.
* According to facebook, the request as we make it here should return a
* token that is valid for 60 days. That means this method should be called
* once every sixty days.
*/
private void authenticationFlow() {
Display display = new Display();
Shell shell = new Shell(display);
final Browser browser;
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
shell.setLayout(gridLayout);
try {
browser = new Browser(shell, SWT.NONE);
} catch (SWTError e){
System.err.println("Could not instantiate Browser: " + e.getMessage());
display.dispose();
display = null;
return;
}
browser.setJavascriptEnabled(true);
GridData data = new GridData();
data.horizontalAlignment = GridData.FILL;
data.verticalAlignment = GridData.FILL;
data.horizontalSpan = 3;
data.grabExcessHorizontalSpace = true;
data.grabExcessVerticalSpace = true;
browser.setLayoutData(data);
final ProgressBar progressBar = new ProgressBar(shell, SWT.MOZILLA);
data = new GridData();
data.horizontalAlignment = GridData.END;
progressBar.setLayoutData(data);
/* Event Handling */
browser.addProgressListener(new ProgressListener(){
public void changed(ProgressEvent event){
if(event.total == 0) return;
int ratio = event.current * 100 / event.total;
progressBar.setSelection(ratio);
}
public void completed(ProgressEvent event) {
progressBar.setSelection(0);
}
});
browser.addLocationListener(new LocationListener(){
public void changed(LocationEvent e){
// Grab the token if the browser has been redirected to
// the login_success page
String s = e.location;
String token_identifier = "access_token=";
if(s.contains("https://www.facebook.com/connect/login_success.html#access_token=")){
access_token = s.substring(s.lastIndexOf(token_identifier)+token_identifier.length(),s.lastIndexOf('&'));
String expires_in = s.substring(s.lastIndexOf('=')+1);
expirationTimeMillis = System.currentTimeMillis() + (Integer.parseInt(expires_in) * 1000);
}
}
public void changing(LocationEvent e){}
});
if(display != null){
shell.open();
browser.setUrl("https://www.facebook.com/dialog/oauth?"
+ "client_id=" + APP_ID
+ "&redirect_uri=https://www.facebook.com/connect/login_success.html"
+ "&scope=" + PERMISSIONS
+ "&response_type=token");
while(!shell.isDisposed()) {
if(!display.readAndDispatch()){
display.sleep();
if(access_token != null && !access_token.isEmpty()){
try{ Thread.sleep(3000);}catch(Exception e){}
shell.dispose();
}
}
}
display.dispose();
}
}
So all you have to do is figure out what permissions you're going to need to have for your application to work. Be aware that "second dialog" permissions can be picked from by the user so there is no guarantee that you will actually have these permissions.
First off, you need to get the access_token and then I would recommend using restfb library. In order to get the token I would recommend reading this: https://developers.facebook.com/docs/authentication/
A simple summary:
HTTP GET: https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream&
response_type=token
Use the code you get from above and then, do: https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&
client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE
Exact the access_token and use FacebookClient from restfb to make the API requests.

Categories