I have developed a application in java that connects to facebook on the basis of access token. I can produce access token manually by feeding in urls in browser.

But I need something that automatically give me an access token. I am getting no idea, whether I can do it by embedding a browser in java or I need to write any script(which language to use) for doing so.

Some guidance is required for getting this done.

Correct me if I'm wrong anywhere...
Can't you just create an application on FB that can be contacted through your client?
It's safer that way; and I guess it solves your access token problem. That's how most of the mobile clients work!

Correct me if I'm wrong anywhere...
Can't you just create an application on FB that can be contacted through your client?
It's safer that way; and I guess it solves your access token problem. That's how most of the mobile clients work!

You are some what right. I am using a mechanism called X-Facebook to authenticate and make my app work on facebook, which need a app to be registered on facebook and your native app access facebook through that app registered on facebook and for that you need an access token which is generated after user authorize your app on facebook.

So in short that access token is required my native application to communicate with facebook further.

ohk... Got it!

http://developers.facebook.com/docs/authentication/

does this help?

I have no problem getting an access token manually. Till now for developing I was fetching the access token manually but now I need to embed this process in my application as well. So I was wondering how should I make my application written in java to communicate with web browser which ask user to authorize my app, which in turn gives me a code which is then used by me in same http method to get the access token.,

I have no problem getting an access token manually. Till now for developing I was fetching the access token manually but now I need to embed this process in my application as well. So I was wondering how should I make my application written in java to communicate with web browser which ask user to authorize my app, which in turn gives me a code which is then used by me in same http method to get the access token.,

By reading documentation!
Its all there and if you not willing to search, we will not be doing for you. You ask general question you get general answer

By reading documentation!
Its all there and if you not willing to search, we will not be doing for you. You ask general question you get general answer

I know how to do all this regarding facebook. I am just asking how can i link my native java application with a web browser to make this happen or how can i embed a web browser in my application. I not only need to open a web browser, I want the result url of the web browser to proceed further.

Which will be more appropriate?

You do not need to connect your application with web browser, you need to get user credentials, submit them to facebook and if accepted then query for other data you want.

You do not need to connect your application with web browser, you need to get user credentials, submit them to facebook and if accepted then query for other data you want.

It is not like that, it is against facebook policies, facebook uses OAuth protocol so I have to go through browser's say.

And I am not asking for alternatives, I am here to ask you people how to get this done what I am asking.

I have a java application that is going to authorize itself on facebook by the user of the application. And according to facebook policies I have to do this by directly connecting to facebook, instead of asking for user's facebook credentials.

Scenario is like this:
1) Open a link in a web browser that directs user to a page of facebook to authorize the app.

2) When user press I Agree on that page,it gives a code in the address bar of web browser. After pressing I Agree interface for user has to be closed.

3) I have to take this code returned in address bar to open another HTTP request to facebook which opens a html page with some code over it, I need to get that code. After step 2, there should be no interface for user, or the browser need to be closed automatically then.

Now, can anyone please tell me how can I achieve this. Do I need to use any additional language with JAVA or is it possible to embed a web-browser in my java application, so I can control everything internally. As I not only need to open a web browser with a link, I also need the responses of the user's work.

As per what I understood after skimming through the documentation, FB will generate a token for you and you need not do so! Am I right?

As per what I understood after skimming through the documentation, FB will generate a token for you and you need not do so! Am I right?

Yeah, and I am doing all this just to get that token in my java app

http://YOUR_URL?code=A_CODE_GENERATED_BY_SERVER

With this code in hand, you can proceed to the next step, app authentication, to gain the access token you need to make API calls.

In order to authenticate your app, you must pass the authorization code and your app secret to the Graph API token endpoint at https://graph.facebook.com/oauth/access_token. The app secret is available from the Developer App and should not be shared with anyone or embedded in any code that you will distribute (you should use the client-side flow for these scenarios).

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

If your app is successfully authenticated and the authorization code from the user is valid, the authorization server will return the access token

got it?

The following HTML/JavaScript example demonstrates the client-side flow in one self-contained example:

<html> 
   <head> 
     <title>Client Flow Example</title> 
   </head> 
   <body> 
   <script> 
     function displayUser(user) {
       var userName = document.getElementById('userName');
       var greetingText = document.createTextNode('Greetings, '
         + user.name + '.');
   userName.appendChild(greetingText);
     }

     var appID = YOUR_APP_ID;
     if (window.location.hash.length == 0) {
       var path = 'https://www.facebook.com/dialog/oauth?';
   var queryParams = ['client_id=' + appID,
     'redirect_uri=' + window.location,
     'response_type=token'];
   var query = queryParams.join('&');
   var url = path + query;
   window.open(url);
     } else {
       var accessToken = window.location.hash.substring(1);
       var path = "https://graph.facebook.com/me?";
   var queryParams = [accessToken, 'callback=displayUser'];
   var query = queryParams.join('&');
   var url = path + query;

   // use jsonp to call the graph
       var script = document.createElement('script');
       script.src = url;
       document.body.appendChild(script);        
     }
   </script> 
   <p id="userName"></p> 
   </body> 
  </html>

This should make it clear. You should have scoured the documentation properly! It's not tough getting the token into your app now, since the server is returning the following text:-

access_token=x

Can't you obtain x from that?

@dantinkakkar: I am not facing problem with getting access token, also not with the authentication, I also read the documentation and get it all done.

The question I am asking in this post is how to get that access token programatically from my java application.

wait i understood your problem
Do you have knowledge of TCP/IP Networking?

wait i understood your problem
Do you have knowledge of TCP/IP Networking?

Yeah, i have knowledge of it. Why?

yeah so if you do, i don't see any reason why you can get the token in your app!? seriously this is getting murkier.
see, you can communicate with the FB servers using a simplistic java client. don't expect me to write any code.
just

import java.net.*; //right, i guess?

yeah so if you do, i don't see any reason why you can get the token in your app!? seriously this is getting murkier.
see, you can communicate with the FB servers using a simplistic java client. don't expect me to write any code.
just

import java.net.*; //right, i guess?

I also thought about the same, but I am not sure will it work this way or not because in the authentication facebook uses https, doesn't it pose any problem with simple TCP classes?

I am getting your point. And I know it can be done with networking classes but only when you have that code in your hand that comes as URI when user authorizes an app.

As Facebook documentation says for desktop apps:

Our OAuth 2.0 implementation does not include explicit desktop app support. However, if your desktop app can embed a web browser (most desktop frameworks such as .NET, AIR and Cocoa support embedding browsers), you can use the client-side flow with one modification: a specific redirect_uri. Rather than requiring desktop apps to host a web server and populate the Site URL in the Developer App, we provide a specific URL you can use with desktop apps: https://www.facebook.com/connect/login_success.html.

Embed a web browser and load the OAuth Dialog (https://www.facebook.com/dialog/oauth) using the client-side flow (i.e. response_type=token):

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=https://www.facebook.com/connect/login_success.html

After the user authorizes your app, we redirect the user back to the redirect_uri with the access token in the URI fragment:

And this is what I am asking, how to embed a web browser to make a user log into his facebook account and authorize my app. I tried building a simple JEditorPane based web-browser but it is not good enough for the job.

And can JSP or servelets or JavaScript can be of any use in opening a web browser and then supplying the data back from the web browser to java application?

Now, listen. I got the solution.
The HTTPS protocol will require just a little addition to the previous TCP/IP code. Otherwise, the procedure remains the same!

Now, listen. I got the solution.
The HTTPS protocol will require just a little addition to the previous TCP/IP code. Otherwise, the procedure remains the same!

Care to share some code example for future visitor to understand what you mean?

Use the RestFB java library. It's open source. I use it my Java EE app. Very easy.

I will not answer your question because I haven't figured out how to do it yet but android SDK has the WebViewClient class that implements a method shouldOverrideUrlLoading which is called everytime a url is loaded and returns true if the control should return to the application loaded the webview (if login fails you don't necessarily want to return). If you want to return a particular result from url you should have a listener in this class. One way to do it might be to reverse engineer the way this class works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.