OAuth 2.0 with Google Calendar on Web Application - java

I'm having problems with the URL callback for Google Calendar for a Java web application. I followed the docs successfully and got this working locally but I am now trying to run this on an EC2 instance.
The quickstart tutorial on Google says to select Application Type as 'other'. This worked fine for me locally.
However, when I try and run this on a server I am given the URL to follow in the logs and the URL has a callback with localhost over a different port every time. Since the app was no longer running on localhost, the callback was never received.
After some reading, I need to set the credentials to be 'web application' when actually running this on a web server. This states that I need to define a 'Authorised redirect URIs' and needs to have the correct port.
I was using the following, based on the tutorial for the redirect to the URL to authenticate the user:
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
After reading this SO post I then set the port in code so I could enter the same port in the credentials to then go into the client_secret.json (my app is currently running on 8080)
I am doing this via:
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver.Builder().setPort(8080).build()).authorize("user");
However, this throws a bind exception since obviously the port is already in use.
Choosing a port I know is free (I tried 9999) does take me through to the page to allow me to authenticate which makes sense since the URI matches the one stated in the client_secret.json. This then throws a socket bind failure exception:
Caused by: java.lang.Exception: Socket bind failed: [730048] Only one
usage of each socket address ( protocol/network address/port) is
normally permitted.
A lot of examples I have seen online have not set the port, which then allows the AuthorizationCodeInstalledApp to pick a free port but how will this work when we need to specify the Authorised redirect URIs in the API Credentials?

I found the solution for this. I actually had something else running on port 9999 which is why the above didn't work locally. The AuthorizationCodeInstalledApp builder has a setHost() method which works for the EC2 instance.
Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver.Builder().setHost(REDIRECT_URI).setPort(9999).build()).authorize("user");

Related

ERR_CERT_AUTHORITY_INVALID when accessing same hostname with different port

We have a splunk instance which is exposed to internet via say https://splunk.mycompany.com
When we access the above URL browser says connection is secure meaning all certificates are ok.
Now splunk REST API service is running on port 8089. So to access splunk REST API we have to hit
https://splunk.mycompany.com:8089
Whenever we are hitting the above URL we are getting certificate issues and browser is saying "your connection is not private"
Error is: NET::ERR_CERT_AUTHORITY_INVALID
As I am still accessing the same hostname via https (and a new port) it should establish a secure connection. But why it's failing to validate certificate authority?
Edit: I have been told by the splunk team to take ther certificate of https://splunk.mycompany.com and install in the java keystore in the machine from where the REST API call is being made. They also told this is working for otheres. My question why it is even needed?
You should enable SSL on port 8089 via server.conf file.
Have a look at the Splunk Documentation here: https://docs.splunk.com/Documentation/Splunk/9.0.0/Security/ConfigTLSCertsS2S

Can not connect from flutter app to java backend running on Google Cloud Run

I have deployed a java backend in a docker container running on Google Cloud Run, now am having connection issues. I can reach my java backend using chrome and postman, but not flutter.
I am connecting to it using a flutter app. My java backend uses Jetty embedded for http. Previous to this, I did development on my local machine, and in the flutter app I would hard code in my servers LAN IP. The client connected reliably in this scenario
I have since deployed to Google Cloud Run and am not able to connect to the backend with my client.
I have tested the docker container locally, and I was able to connect to my backend when running the container using this command...
docker run -p 8080:8080 --network="host" image1
In the google cloud run console, I set the port to 8443.
In my flutter app. I have changed the URL from...
https://192.168.100.103:8080
To the url the Google Cloud Run specifies as my IP (I then added the port)...
https://blahblah-82j3flsijf-uc.a.run.app:8080
My flutter code that establishes the connection is as follows...
HttpClient client = HttpClient();
client.connectionTimeout = Duration(seconds:2); // throws SocketException after timeout
client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true); // find the way to specifically accept a self signed certificate
HttpClientRequest request = await client.getUrl("https://blahblah-82j3flsijf-uc.a.run.app:8443");
request.headers.contentLength = requestBody.length;
request.write(requestBody);
My java backend code that establishes the jetty http server is as follows....
Server server = new Server(8080);
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{ new MessageHandler(), new DefaultHandler()});
server.setHandler(handlers);
server.start();
server.join();
When I try to connect from my flutter client, I get http 400 bad request.
I solved my issue. I had a few problems here.
First was that I was using https encryption in my java app when Google Cloud Run takes care of that for me. So I disabled https in my java app. I assume Google Cloud Run requires your container to handle only http requests (not positive though).
Secondly, I changed all my ports to run the Google Cloud Run default of 8080, and then completely removed the port from my flutter url.
Thirdly, I was calling HttpClient.getUrl which performs an http GET, but I was also adding a request body to the get. You should not add a body to a GET request. This previously worked for me because both my jetty based java backend, and the flutter dart:io HttpClient are completely fine with this violation, and both worked with a GET with a body. As soon as I introduced Google Cloud Run layer, it introduced more stringent enforcement of the http protocol, and correctly returned bad request when I sent a GET with a body.
If you are adding a body, use dart:io HttpClient.postUrl.

Utilising SAS Token Authentication in Java SE client

I am writing a simple Java client that goes to a workspace server to retrieve some data. Workspace server does not allow public connections and the authentication mechanism is "SAS Token Authentication".
When I attempt to connect to the server, I get an error for invalid credentials. The same set of credentials work just fine when I attempt to connect to SAS Metadata server in Java code. I have full access to the workspace in questions in SAS EG.
SAS institute (I filed a support track) says that my connection needs to be SAS Metaserver aware, in other words I should have an active connection with SAS Metadata server and then connect with the same credentials to a workspace server of choice. The workspace server in turn will validate credentials against a list of active connections on Metadataserver.
I can do the first part no problem, I get a connection with metadata server going, but when I attempt to connect to a workspace server while maintaining connection with metadata, my connection is denied saying username/password are incorrect.
I have used this sample code where I deleted methods for creating data tables/columns on metadata and instead put another method that starts a workspace factory with the same credentials after initiating a successful connection with metadata server, but before closing it. http://support.sas.com/documentation/cdl/en/omaref/63063/HTML/default/viewer.htm#p0zlnmq1vv8xgbn1hshp4rmw2btd.htm
I had posted this on LinkedIn where some suggestions were around token authentication, that I don't need to send a second set of credentials and I should obtain a token from metadata, use that to connect. The only reference to a token object that I found is on here: http://support.sas.com/rnd/javadoc/93/Foundation/com/sas/services/user/UserContextInterface.html#SHAREDRESOURCEKEY_ENVIRONMENT
Any general suggestions? Have I got something conceptually wrong here? Does anyone have a working sample of connecting to workplace server which uses SAS Token Authentication?
Regards,
Vasilij

How to connect to proxy server using Java

I want to write a java(SE) program to connect to a proxy server, lets say 123.123.123.123:8080. How am I going to achieve that? What is the protocol between my machine and the proxy server? What is the Java framework's class could be in use?
since java 1.5,you can use java.net.Proxy class to create proxy.
Proxy proxy=new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.123.123.123", 8080);
URL url = new URL("http://www.example.com");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();
reference
The definitive reference for network proxy configuration in Java 5 is this Java Networking and Proxies page.
Yes proxy server is a web server...
Whenever u send a request through your browser to get some resource in the particular web server(say www.google.com),the request is send to the proxy server instead to sending the request directly to the google server..the proxy server process this request,send them to the gooogle server,receives the response and then send the response back to the browser.
Proxy server is basically used to corporate fields to restrict the accesss to specific websites,to keep a track of the internet used by a particular associate,Also it saves some commoonly used webpages in a cache file,so that when another request comes,then instead of connecting to the required server,it get the webpage fron the cache file..Hence it saves the time.Also it scans the incoming data from any server for malware before submitting it to the client(browser).To check if ur company is using proxy server,u can go to the internet explorer setting ->Connections ->LAN Settings

How to Connect to a web service (SSL enabled and proxy interface)

There is a web service running on tomcat on a server. It is built on Java Servlet. It is listening others to call itself on a SSL enabled http port. so its web service adreess looks
like: https://172.29.12.12/axis/services/XYZClient?wsdl
On the other hand I want to connect the web service above from a windows application which is built on .NET frame work.
Finally, when I want to connect the web service from my computer; I get some specific erros;
Firstly I get; Proxy authentication error; then I added some new line to my code;
Dim cr As System.Net.NetworkCredential = New System.Net.NetworkCredential("xname", "xsurname", "xdomainname")
Dim myProxy As New WebProxy("http://mar.xxxyyy.com", True)
myProxy.Credentials = cr
Secondly, after this modifications It says that bad request.
I did not get over this error.
Moreover I did try to connect the web server on the same computer. I copied my executable program to the computer where the web service runs. The error was like;
The underlying connection was closed:
Could not establish trust relationship
for SSL/TLS secure channel
PS: When I try to connect to web service by using Internet Explorer; I see firstly some warnings about accepting an unknown certificate and I click take me to web service an I get there clearly.
I want to know what are the basic elements to connect a web service, could you please tell me the requirements that I have to use on my windows project.
regards bk
This page offers a simple step-by-step guide on building a web service client with C#.NET.
Step 5 on this page shows how to add a certificate to your trusted store and Step 6 is another way to build a very simple web service client in C#.NET.

Categories