I'm building a Java Application that will connect to the Facebook and get some information. To connect to the Facebook API, Facebook asks for a code called "AccessToken", which you can get when the user connects to your application. My application is running 100% perfect when I already have the AccessToken, but I'm currently getting it externally.
So, I read the Facebook docs and apparently the only way to get the AccessToken is using a Browser, even if it is a Desktop application (like my application). I certainly would not like to run an embbed browser in my application (My objective is to build the entire interface using only Swing components).
So, my question is: In my case, is there a way to me to get this AccessToken without the need of a Browser? Or I REALLY need to embbed a webbrowser in my application or make the user open a browser and get it externally like I'm doing?
Thank you!
Related
I have a java (Spring boot) web service which does not have any UI.
I want to send a dynamic Email (created using Thymeleaf and injecting values from a database) using my web service on a daily Cron schedule, using Microsoft Graph and O365 APIs.
Right now I use SMTP to send emails, but I cannot use it anymore as that is no longer going to be supported by the O365 account that I am using.
I found the SendMail APIs on the Graph Documentation which looks pretty straightforward.
But, using the Graph AIPs requires you to create an Azure AD project first and use their Microsoft Identity platform - which I created.
Now, the problem is that most of the flows also require a user to manually login from a login window.
This is where things get complicated.
I do not have a place to show a Login window to any user from my web service, because it is just a backend service there is no UI. I intend to use a service account for sending the emails through the Application.
I found a Daemon support as well, but it seems to only support Python or .Net code.
Migrating my code from Java to either of those platforms just to be able to send emails
does not feel like a good solution.
And, I'm not even sure if they even offer similar capabilities of sending dynamic emails like Java+Thylemeaf do?
Is there a way to be able to continue doing this using my existing code in Java?
If not, then as the worst case scenario, are there any libraries in Python which can allow me to send dynamic emails like thymeleaf does in Java?
As you don't want to manually login from a login window, you can use the client credential flow.
Here is the guide regarding how to access graph api without user.
Reference:
msgraph-sdk-java-auth (You can choose to use Client credential provider)
I have created a dummy script to avoid scope issues when trying to run it from Java code. Then, I check advanced google services under resources and go to the API console to try to deal with the credentials.
I can see 'Apps Script' credentials have been already created for me. When I try to set the redirectURI to point an endpoint I am exposing and then save, I get an error message saying I have no rights to perform the action.
Is there any code example to run google scripts from java? I have tried the tutorial but it is not focusing in the authorization. I have been so far able to create the script from java code with the token I get from credentials I have created, but not to run the scripts.
I am working on a desktop project that requires Google Authorization for Google Calendar API using OAuth2.0. The language used is java.
Currently to authorize the project, I have to open the browser using java.swt.Desktop.getDesktop().browse(url), click the "accept" button, copy the authorization code in the redirected page and paste it in my application. And the user has to close the webpage manually. Quite troublesome.
Is there a better, more user-friendly approach to achieve this authorization procedure? The ultimate procedure I want to achieve is only that the application opens the authorization page, the user clicks "accept", the page closes itself automatically and the software is authorized. I have seen this kind of procedure in other applications. It's just that I don't know how to achieve this.
Thanks in advance.
You need to provide a callbackURL. I would recommend using a java library like Scribe to accomplish this - there is a good getting started page and plenty of examples.
EDIT 1
Here is a sample for setting up a callback url
String apiKey = "your_app_id";
String apiSecret = "your_api_secret";
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey(apiKey)
.apiSecret(apiSecret)
.callback("myApp://oauthcallback")
.build();
You would need to open an embedded browser in your desktop app, and go to the oauth url as given by OAuthService. Once the user has approved your app, the embedded browser will be redirected to your callback URI. You'll need to detect this and then extract the oauth info included in the callback.
I've never done this through a desktop application (it was always within a servlet/jsp - web app). But if you search for how to detect redirect on a URI in windows, hopefully you will find some examples.
I have a web PHP web application that has a link to a java web application. The php application has a login page, and a link to the the java application, but not every user has permission to access the java web application. What I was trying to do is send user credentials from the php application to the java application, and then the java application checks the credentials and if correct logs in the user. I was thinking of using http headers to do this.
So my question is what is how to send user credentials from a PHP application to a java application?
If it helps I am using a Java web framework called Vaadin.
Do a normal POST request from the PHP application to the java application. This can be done as simply as having a normal HTML form in the PHP application, set the form's method to "POST" and action to the java application's URL. If you want to catch HTTP parameters in a Vaadin application, you can do it by using request handlers (https://vaadin.com/book/vaadin7/-/page/advanced.requesthandler.html).
Then a few words of advice or something to at least consider. If your login page is in the PHP application and your "admin" application is the Vaadin application, then I discourage you from doing the credential checking in the Vaadin application. This is because when you enter the Vaadin application, a new application instance is created. This means that your UI will be initialized and whatever else you do in the UI's init method. What you probably want to do, is to hinder the user from entering the Vaadin application unless she is logged in - which means that you need to do the credential checking somewhere else - for example, have a separate servlet whose only responsibility is to log in the user. If login is granted, then give access to the Vaadin application, if access is denied, forward the user to the PHP login screen. The next question is, how do you hinder the user from accessing the Vaadin application until she is logged in? Typically, this is done using servlet filters.
I highly encourage you to use a 3rd party framework for doing the authentication and authorization. Take a look at http://shiro.apache.org/, it's easy to install and seems to work nicely together with Vaadin. All you need to do is to configure it and implement a login screen, the framework will take care of the rest.
If I understood your question, you want to be able to provide an "auto-login-link" to some specific users that are logged in to the PHP application. This link should automatically login the user to the java application, right?
Without knowing any details about this case, like are both apps running on the same domain or do they use the same database (same user credentials in both apps), etc., I would propose the following solution:
Create an action (link) on the java application, which receives the necessary parameters (as GET) needed for creating the session (probably userId is sufficient), timestamp and a signature of all parameters. For example:
http://javaapp.example.com/autologin?userId=123&timeStamp=123456789&sign=hj23kh4j234jk324h
Where the signature is calculated with some strong encryption algorithm. Then you verify that the signature is correct at the receiving end (java app). If it is correct, you create the session. Signature calculation could be something like:
$signature = sha1($userId . $timeStamp . 'some salt' . $sharedSecretBetweenBothApps);
With the timeStamp you are able to check that an old link is not used. For example not allow older than 15 min old links and store used links in the java app to make sure they are never re-used. You do not have to keep history of links older than the expiration time.
Another idea, as discussed in the comments, is creating an API on the java side, which is able to provide a one-time link.
The sha1 algorithm is probably not strong enough, but shows the idea and is simple to implement.
Does this answer your question?
How do I login into Facebook and get a accesstoken in Java with a desktop application only?
I found a api http://restfb.com that lets you call methods but you have to get the token yourself. So is there a way to do this using only a desktop app? Maybe embed a browser to a window popup?
I already know the process in getting a token explained here http://benbiddington.wordpress.com/2010/04/23/facebook-graph-api-getting-access-tokens so my question doesnt involve that. I need a way for the user to login into facebook using some kind of GUI.
As in the facebook document
Our OAuth 2.0 implementation does not include explicit support for application built for desktop operating systems. However, if your 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.
I think you are right about embed web browser.
And I suggest you to move to Graph API instead of Rest, Because It's deprecating.
see more abount Facebook Authentication
Deprecating the REST API