Yandex Api not translating properly in eclipse - java

Here is my code in TranslateAPI.java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
public class TranslateAPI {
public static final String API_KEY = "pdct.1.1.20180924T090857Z.3e14b8b207704aef.9bdc409229b123003526815bb7062ed42616f26a";
private static String request(String URL) throws IOException {
URL url = new URL(URL);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
InputStream inStream = urlConn.getInputStream();
String recieved = new BufferedReader(new InputStreamReader(inStream)).readLine();
System.setProperty("http.agent", "Chrome");
String agent = java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("http.agent"));
inStream.close();
return recieved;
}
public static Map<String, String> getLangs() throws IOException {
String langs = request("https://translate.yandex.net/api/v1.5/tr.json/getLangs?key=" + API_KEY + "&ui=en");
langs = langs.substring(langs.indexOf("langs")+7);
langs = langs.substring(0, langs.length()-1);
String[] splitLangs = langs.split(",");
Map<String, String> languages = new HashMap<String, String>();
for (String s : splitLangs) {
String[] s2 = s.split(":");
String key = s2[0].substring(1, s2[0].length()-1);
String value = s2[1].substring(1, s2[1].length()-1);
languages.put(key, value);
}
return languages;
}
public static String translate(String text, String sourceLang, String targetLang) throws IOException {
String response = request("https://translate.yandex.net/api/v1.5/tr.json/translate?key=" + API_KEY + "&text=" + text + "&lang=" + sourceLang + "-" + targetLang);
return response.substring(response.indexOf("text")+8, response.length()-3);
}
AND in workerthread.java:
String s=TranslateAPI.detectLanguage(abc);
System.out.println(s);
However,I am getting the follwing errror:
Server returned HTTP response code: 403 for URL: https://translate.yandex.net/api/v1.5/tr.json/detect?key=pdct.1.1.20180924T090857Z.3e14b8b207704aef.9bdc409229b123003526815bb7062ed42616f26a&text=cat
Can you please help? Thanks in advance

You are getting a 401 Error thus Your API Key is Invalid,
You can always go to Yandex's Developers page to get a new one.
Its always great to publish your private API here :D

Related

Convert url encoded data to json

I expected to get JSON data from a webhook.
I get this form of data below and the content/type was application/x-www-form-urlencoded instead of application/json
results%5B6%5D%5Bid%5D=7&results%5B18%5D%5Bid%5D=19&results%5B0%5D%5Bname%5D=data+autre&results%5B1%5D%5Bname%5D=data2+autre&assessments%5B0%5D%5Bstatus%5D=finish&results%5B10%5D%5Bscore%5D=6&results%5B7%5D%5Bname%5D=data3&results%5B6%5D%5Bname%5D=Accept&results%5B8%5D%5Bname%5D=data4&results%5B2%5D%5Bname%5D=autres&results%5B3%5D%5Bname%5D=data6&results%5B4%5D%5Bname%5D=autre&results%5B5%5D%5Bname%5D=autres3&results%5B9%5D%5Bname%5D=data8&results%5B17%5D%5Bid%5D=18&reports%5B4%5D%5Bid%5D=8&reports%5B4%5D%5Bis_available%5D=0&results%5B7%5D%5Bscore%5D=7&results%5B17%5D%5Bscore%5D=4&reports%5B1%5D%5Bis_available%5D=1&assessments%5B2%5D%5Blink%5D=https%3A%2F%2Ftest%3D123&lastname=aaa&results%5B3%5D%5Bscore%5D=10&reports%5B3%5D%5Bid%5D=15&results%5B16%5D%5Bid%5D=17&register_link=&results%5B7%5D%5Bid%5D=8&results%5B19%5D%5Bid%5D=20&results%5B13%5D%5Bscore%5D=5&assessments%5B1%5D%5Bstatus%5D=todo&results%5B4%5D%5Bid%5D=5&status=accepted&results%5B9%5D%5Bid%5D=10&results%5B15%5D%5Bid%5D=16&results%5B3%5D%5Bid%5D=4&reports%5B4%5D%5Bname%5D=data9&reports%5B3%5D%5Bname%5D=data10&results%5B18%5D%5Bscore%5D=1&email=test#test.com&results%5B9%5D%5Bscore%5D=6&synthesis=
How can I convert this to json ?
Thanks
if you are looking to convert this in java, may be you can try the following code:
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class URLEncodeDecode {
public static void main(String[] args) {
String url2 = "results%5B6%5D%5Bid%5D=7&results%5B18%5D%5Bid%5D=19";
String decodeURL = decode(url2);
System.out.println("Decoded URL: " + decodeURL);
System.out.println(Stream.of(decodeURL.split("&")).map(elem -> new String(elem)).collect(Collectors.toList()));
List<String> uriToList = Stream.of(decodeURL.split("&")).map(elem -> new String(elem))
.collect(Collectors.toList());
Map<String, String> uriToListToMap = new HashMap<>();
for (String individualElement : uriToList) {
uriToListToMap.put(individualElement.split("=")[0], individualElement.split("=")[1]);
}
// Use this builder to construct a Gson instance when you need to set
// configuration options other than the default.
GsonBuilder gsonMapBuilder = new GsonBuilder();
Gson gsonObject = gsonMapBuilder.create();
String uriToJSON = gsonObject.toJson(uriToListToMap);
System.out.println(uriToJSON);
}
public static String decode(String url) {
try {
String prevURL = "";
String decodeURL = url;
while (!prevURL.equals(decodeURL)) {
prevURL = decodeURL;
decodeURL = URLDecoder.decode(decodeURL, "UTF-8");
}
return decodeURL;
} catch (UnsupportedEncodingException e) {
return "Issue while decoding" + e.getMessage();
}
}
}

kerberos fallback to ntlm java code

I am working on Microsoft Sharepoint integration with SAP. The sharepoint supports 'kerberos fallback to ntlm' method for authentication.
I wish to take advantage of ntlm authentication, as I dont wish to go into kerberos set up. My understanding is that ntlm authentication can be managed with following piece of code however, I am getting '401 Unauthorized' error.
Probably something needs to be added to following line in password authentication.
if (getRequestingScheme().equalsIgnoreCase("negotiate") || getRequestingScheme().equalsIgnoreCase("ntlm") || getRequestingScheme().equalsIgnoreCase("kerberos"))
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.lang.Object;
import java.nio.charset.StandardCharsets;
import java.io.OutputStream;
import java.net.InetAddress;
public class Main {
public static void main(String[] argv) throws Exception {
Authenticator.setDefault(new MyAuthenticator());
URL url = new URL ("https://sharepointlink");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
//conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty("Accept", "*/*");
//MyAuthenticator m = new MyAuthenticator();
String urlParameters = "tests";
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
byte[] data = ("").getBytes("UTF-8");
OutputStream out = conn.getOutputStream();
out.write(postData);
out.flush();
StringBuilder response = new StringBuilder();
InputStream stream = conn.getInputStream();
// InputStream estream = conn.getErrorStream();
BufferedReader in = new BufferedReader(new InputStreamReader(stream));
String str = "";
while ((str = in.readLine()) != null) {
response.append(str) ;
}
in.close();
System.out.println(response.toString());
// return response.toString() ;
}
}
class MyAuthenticator extends Authenticator {
// static String user = System.getProperty("user");
// static String pass = System.getProperty("pass");
// static String kuser = System.getProperty("kuser");
// static String kpass = System.getProperty("kpass");
// static String showhint = System.getProperty("showhint");
protected PasswordAuthentication getPasswordAuthentication() {
String promptString = getRequestingPrompt();
System.out.println("prompt string " + promptString);
String hostname = getRequestingHost();
System.out.println("host name " + hostname);
InetAddress ipaddr = getRequestingSite();
System.out.println(ipaddr);
int port = getRequestingPort();
RequestorType reqType = getRequestorType();
System.out.println ("reqeust type = " + reqType.toString());
System.out.println ("Protocol type = " + getRequestingProtocol());
System.out.println ("Scheme type = " + getRequestingScheme());
if (getRequestingScheme().equalsIgnoreCase("negotiate") || getRequestingScheme().equalsIgnoreCase("ntlm") || getRequestingScheme().equalsIgnoreCase("kerberos")) {
String krb5user="tp1\\user";
String krb5pass ="pwd";
// get krb5user and krb5pass in your own way
return (new PasswordAuthentication (krb5user, krb5pass.toCharArray()));
}
String username = "TP1\\user";
String password = "pwd";
return new PasswordAuthentication(username, password.toCharArray());
}
}
Can anyone help please.

Skyscanner API example in Java

I am trying to build an example of request for Skyscanner API in Java - but I am doing something wrong - the link for skyscanner API test: http://business.skyscanner.net/portal/en-GB/Documentation/FlightsLivePricingQuickStart
Here is the test code I have so far - I get an "Internal Server Error".
Anyone can see what is incorrect in this example?
Thanks
package FLIGHTS;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
public class Test {
public static final String HTTP_HEADER_X_APPLICATION = "X-Application";
public static final String HTTP_HEADER_X_AUTHENTICATION = "X-Authentication";
public static final String HTTP_HEADER_CONTENT_TYPE = "Content-Type";
public static final String HTTP_HEADER_ACCEPT = "Accept";
public static final String HTTP_HEADER_ACCEPT_CHARSET = "Accept-Charset";
public static String ENCODING_UTF8 = "UTF-8";
public static void main(String[] args) throws IOException {
HashMap<String, Object> params = new HashMap<>();
String API_KEY = "prtl6749387986743898559646983194";
// params.put("apiKey", API_KEY);
params.put("Country", "GB");
params.put("Currency", "GBP");
params.put("Locale", "en-GB");
params.put("Adults", 2);
params.put("Children", 2);
params.put("Infants", 0);
params.put("OriginPlace", 11235);
params.put("DestinationPlace", 13554);
params.put("OutboundDate", "2016-01-23");
params.put("InboundDate", "2016-01-30");
params.put("LocationSchema", "Default");
params.put("CabinClass", "Economy");
params.put("GroupPricing", true);
String url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apikey="+API_KEY;
System.out.println(url);
HttpPost post = new HttpPost(url);
JsonrpcRequest request = new JsonrpcRequest();
request.setParams(params);
request.setMethod("POST");
request.setId("1");
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX").create();
String jsonRequest = gson.toJson(request);
System.out.println(jsonRequest);
post.setHeader(HTTP_HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded");
post.setHeader(HTTP_HEADER_ACCEPT, "application/json" );
post.setHeader(HTTP_HEADER_ACCEPT_CHARSET, ENCODING_UTF8 );
post.setEntity(new StringEntity(jsonRequest, ENCODING_UTF8));
HttpClient httpClient = new DefaultHttpClient();
JsonResponseHandler reqHandler = new JsonResponseHandler();
String resp = httpClient.execute(post, reqHandler);
System.out.println(resp);
}
static class JsonrpcRequest {
private String jsonrpc = "2.0";
private String method;
private String id;
private Map<String, Object> params;
public String getJsonrpc() {
return jsonrpc;
}
public void setJsonrpc(String jsonrpc) {
this.jsonrpc = jsonrpc;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Map<String, Object> getParams() {
return Collections.unmodifiableMap(params);
}
public void setParams(Map<String, Object> params) {
this.params = params;
}
}
static class JsonResponseHandler implements ResponseHandler<String> {
#Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
HttpEntity entity = response.getEntity();
return entity == null ? null : EntityUtils.toString(entity, ENCODING_UTF8);
}
}
}
I know it's an old question but I spent a lot of time to find an example that works in Java so I post here the solution that I've done. I used the HttpUrlConnection insted of the HttpClient and HttpPost classes.
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class FlightsJava {
public static void main(String[] args) throws IOException {
String request = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/";
URL url = new URL( request );
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty( "charset", "utf-8");
String urlParameters = "apiKey=YOUR_API_KEY&country=BR&currency=BRL&locale=pt-BR&originplace=SDU&destinationplace=GRU&outbounddate=2016-09-23&locationschema=Iata&adults=1";
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
try{
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(postData);
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
System.out.println("Header Fields : " + conn.getHeaderFields());
} catch (Exception e){
System.out.println(e);
}
}
}
Remember to change "YOUR_API_KEY" for your apiKey provided by skyscanner.
Hope it helps to anyone.

JSONException Twitter client side application

I am currently trying to build a client application for twitter. One of the functionalities of the app is to search tweet (including historical tweet). I tried to modify the code that I got from Github. However, when I tried to debug the code, I got JSONException cause by null value. Here is my code:
package thematicanalysis;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import twitter4j.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import twitter4j.JSONException;
/**
*
* #author adichris
*/
public class TweetManager {
private static String getURLResponse(String since, String until, String querySearch, String scrollCursor,int counter) throws Exception{
String appendQuery = " ";
if(since!=null)
appendQuery+= " since:"+since;
if(until!=null)
appendQuery+= " until:"+until;
if(querySearch!=null)
appendQuery+= " "+querySearch;
String url = String.format("https://twitter.com/search?src=typd&q=%s&scroll_cursor=%s", URLEncoder.encode(appendQuery, "UTF-8"),scrollCursor);
System.out.println("URL: "+ url);
URL obj = new URL (url);
HttpURLConnection con = (HttpURLConnection)obj.openConnection();
con.setRequestMethod("GET");
//StringBuilder response;
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while((inputLine=in.readLine())!=null)
response.append(inputLine);
in.close();
con.disconnect();
//System.out.println(response.toString());
saveToFile(response.toString(),counter);
return response.toString();
}
private static void saveToFile(String content,int counter) throws IOException
{
try (PrintWriter pw = new PrintWriter("newOutput"+counter+".txt","UTF-8")) {
pw.printf("%s\n",content);
pw.close();
}
}
public static void getTweets (String since, String until, String querySearch) throws JSONException, Exception{
try{
String refreshCursor = null;
int counter = 1;
while(true)
{
String response = getURLResponse(since,until,querySearch,refreshCursor,counter);
JSONObject json = new JSONObject(response);
if(json.equals(null))
System.out.println("hereeee");
counter++;
System.out.println(counter);
refreshCursor = json.getString("scroll_cursor");
Document doc = Jsoup.parse((String)json.get("items_html"));
Elements tweets = doc.select("div.js-stream-tweet");
System.out.println(tweets.size());
if (tweets.isEmpty()){
break;
}
for (Element tweet: tweets){
String userName = tweet.select("span.username.js-action-profile-name b").text();
String text = tweet.select("p.js-tweet-text").text().replaceAll("[^\\u0000-\\uFFFF]", "");
long dateMs = Long.valueOf(tweet.select("small.time span.js-short-timestamp").attr("data-time-ms"));
Date date = new Date(dateMs);
System.out.println(userName);
//saveToFile(text);
}
}
}catch(JSONException e){
e.printStackTrace();
}
}
}

Programmatically Fetching Google+ status updates

Is there a way to programmatically fetch Google+ updates for a user's profile? I can't seem to find much in the documentation at https://developers.google.com/+/api/latest/people and http://developer.android.com/reference/com/google/android/gms/plus/model/people/Person.html about fetching statuses. I would like to fetch the data by making an HTTP request or if there is some sort of SDK for Android that will help me, that would work to.
The API you are looking for is plus.activities.list. This will list the Google+ equivalent of Facebook status updates. The referenced page has example code to get you started.
When accessing the API, you should use the Google API client as documented here.
The following code will be useful to retrieve the Http responses.
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.zip.GZIPInputStream;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class GooglePlusStatusHelper {
public GooglePlusStatusHelper() {
}
public static void main(String... args) {
GooglePlusStatusHelper googlePlusStatusHelper = new GooglePlusStatusHelper();
try {
googlePlusStatusHelper.tagsUsed();
} catch (IOException e) {
e.printStackTrace();
}
}
private void tagsUsed() throws IOException {
URL url = createQuery("users");
Type dataType = new TypeToken<Wrapper<Status>>(){}.getType();
Status status = executeQuery(url, dataType);
System.out.println(status);
}
private URL createQuery(String inputParam) throws MalformedURLException {
String baseUrl = "http://api.example.com/" + inputParam ;
System.out.println(baseUrl);
URL url = new URL(baseUrl);
return url;
}
private Status executeQuery(URL url, Type clz) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
System.out.println("Response Code:" + conn.getResponseCode());
System.out.println("Response Message:" + conn.getResponseMessage());
System.out.println("TYPE:" + conn.getContentType());
InputStream content = conn.getInputStream();
String encoding = conn.getContentEncoding();
if (encoding != null && encoding.equals("gzip")) {
content = new GZIPInputStream(content);
}
String result = new Scanner(content, "UTF-8").useDelimiter("\\A").next();
content.close();
Gson gson = new Gson();
return gson.fromJson(result, clz);
}
}
Status class :
public class Status {
private int count;
private String status;
......
public String toString() {
String result = "\ncount: " + count +
"\status:" + status;
result = result + "\n------------";
return result;
}
}

Categories