Using google translator in Java Application [duplicate] - java

This question already has answers here:
Server returned HTTP response code: 403 for URL Google Translate API
(2 answers)
Closed 5 years ago.
I tried to use the solution in the this link: How to use Google Translate API in my Java application?
The code:
Translator translate = Translator.getInstance();
String text = translate.translate("Hello!", Language.ENGLISH, Language.ROMANIAN);
System.out.println(text); // "Bună ziua!"
but the following error appears in my code:
ava.io.IOException: Server returned HTTP response code: 403 for URL: http://translate.google.com.br/translate_a/t?client=t&text=I%20am%20programmer&hl=en&sl=en&tl=pt&multires=1&prev=btn&ssel=0&tsel=0&sc=1
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at com.gtranslate.utils.WebUtils.source(WebUtils.java:24)
at com.gtranslate.parsing.ParseTextTranslate.parse(ParseTextTranslate.java:19)
at com.gtranslate.Translator.translate(Translator.java:37)
at gotranslate.GoTranslate.main(GoTranslate.java:14)

All previous libraries to use Google tools are blocked, the only way to use them is by using Google Cloud Platform:
Google returned the following error: [403] Please use Translate v2. See http://code.google.com/apis/language/translate/overview.html

Related

Making a HTTPS call to API using Java [duplicate]

This question already has answers here:
Java HTTPS client certificate authentication
(9 answers)
Closed 1 year ago.
I'm trying to consume an API. I understand that standard classes like HttpUrlConnection from the java.net package can be used. However, the API I'm trying to consume requires me to present a signed certificate provided by them. I currently have obtained the signed certificate but I have no idea how to import it into my API call.
Using POSTMAN, post addition of client certificate, I'm able to consume the API.
POSTMAN Certificate configuration
Can someone help me on how to add this client certificate and Private key to my HTTP Request? If it's not possible using standard lib, please suggest an alternative way.
What you're trying to achieve is called Client Certificate Authentication. In Java, the one of the most popular library used for making HTTP requests is OkHttp.
You can check their instruction about Client Authentication here or see how they implement it in their unit test.

Java HMACSha1 WooCommerce Invalid signature - provided signature does not match.","data":{"status":401}

I'm trying to make GET Request to our Woocommerce store but I'm struggling to get the correct signature for the url.
This is so that I can build an Android and iOS mobile app. I've checked the Woocommerce Java API # 'https://github.com/icoderman/wc-api-java' and it throws me off a bit.
I have managed to send successful get requests with postman... and gotten the desired results, this is the postman link url:
https://www.xxx.co.za/wp-json/wc/v3/products?per_page=10&oauth_consumer_key=ck_1111111111111111111111111111111111111111&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1547365367&oauth_nonce=bdMWVEdOwtJ&oauth_version=1.0&oauth_signature=0ztOczZezZhmWEhl0bKS0CP+8TQ&page=2
I managed to find code that helped me to get started on GitHub # https://github.com/rameshvoltella/WoocommerceAndroidOAuth1
I've simply rebuilt the project as a java project and not an Android project and I've added the Woocommerce library as packages to combine it into a single application
I made changes to the HMACSHA1SignatureService.java String bytesToBase64Strin() method to:
private String bytesToBase64String(byte[] bytes)
{
return Base64.getEncoder().encodeToString(bytes);
}
Uses: import java.util.Base64;
These are the read only credentials:
Base Site: www.xxx.co.za
Consumer Key: ck_1111111111111111111111111111111111111111
Consumer Secrete Key: cs_1111111111111111111111111111111111111111
I've also tried alternating between HTTP and HTTPS

Failed to get data when accessing the rest-api method getAll [duplicate]

This question already has answers here:
No 'Access-Control-Allow-Origin' header is present on the requested resource- AngularJS
(12 answers)
Closed 5 years ago.
I'm developing an application which has UI from Angular2 and back-end as a rest api which is done on Spring-boot.
When I access the URL to get the list of users, it doesn't fetch all the users. When I check the browser console I get the following error:
Failed to load http://localhost:8080/api/user/getAll: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
Any input will be appreciated.
If the frontend (Angular2) and backend (Boot) live on two different domains a solution would be to enable cors on the backend. Read here: https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/cors.html and here https://spring.io/guides/gs/rest-service-cors/

Getting 400 bad Request with Tomcat 8.5.9 [duplicate]

This question already has answers here:
How to pass a JSON array as a parameter in URL
(9 answers)
Closed 5 years ago.
I'm new to tomcat server technology. Currently I'm working on the spring boot application and I tried calling the below api with the tomcat servers (below 8.5v) running in the background, I got the response as I expected. But when I tried to call the same api with tomcat server 8.5.9v running in the background I'm getting the 400 bad request.
http://localhost:8080/TestRest/ExtractTest?jsonString={"extract":
{"Type":"veswanth", "objects":[{"object":"WTT"}]}}
And in the log file I found the below issue
service Error parsing HTTP request header Note: further occurrences of
HTTP header parsing errors will be logged at DEBUG level.
java.lang.IllegalArgumentException: Invalid character found in the
request target. The valid characters are defined in RFC 7230 and RFC
3986
Kindly help me to fix this issue and correct me if did anything wrong..
You can not pass json data in url in that way. You need to pass it in body and request method should be POST.
You can refer this:
How to pass a JSON array as a parameter in URL

How to get the real home page of a website [duplicate]

This question already has answers here:
Get domain name from given url
(17 answers)
Closed 7 years ago.
I want to know if there is any way to get the real URL of a webpage. For example, I have a website named http://www.tutorialspoint.com/java/java_files_io.htm, the real/main URL is http://www.tutorialspoint.com of its main page.
Now I have another website http://www.sinhgad.edu/sinhgad-engineering-institutes/SEI-placement/index.html, its main page URL is http://www.sinhgad.edu/sinhgad-engineering-institutes/ in place of http://www.sinhgad.edu/ only.
I want to get this main page URL or real URL in Java.
In Java, you can use InetAddress.getLocalHost() to get the Ip Address of the current Server running the Java app and InetAddress.getHostName() to get Hostname of the current Server name.
source.

Categories