I am accessing View API through a HTTP Rest Call from a java class. I am getting the response as
“0|Access denied (702)”
Please help me.
MY Code is as follows:
package org.demo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.net.ssl.HttpsURLConnection;
public class VyewTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String sCmd = “login”;
String sResult = VyewAPI(sCmd);
System.out.println(sResult);
String[] sSplitResult = sResult.split("|");
if (sSplitResult[0].trim() == “1")
{
String sCode = sSplitResult[1];
String sURL = sSplitResult[2];
}
else
{
//API failed
System.out.println("Vyew API failed, retval=” + sResult);
}
}
private static String VyewAPI(String sCmd) throws IOException
{
String sApiUrl = “https://vyew.com/api”;
String sKey = “MY_API_KEY”; //my Vyew API key //e7353293b1
String sSecret = “MY_API_SECRET”; //my Vyew API secret phrase
long nEpochTime = new Date().getTime()/1000;
String email= “MY_ACCOUNT_EMAIL_ID”;
String pw="MY_ACCOUNT_PASSWORD";
String sQ = “cmd=” + sCmd;
sQ += ("&email;="+email);
sQ += ("&pass;="+pw);
sQ += ("&key;=" + sKey);
sQ += ("&time;=" + Long.toString(nEpochTime));
String sMD5 = GetMD5Hash(sQ + sSecret);
String sURL = sApiUrl + “?” + sQ + “&md5;=” + sMD5;
String postData = “dbQuery=” + URLEncoder.encode(sURL, “UTF-8");
System.out.println(sURL);
String sResponse = “”;
URL u = new URL(sURL);
URLConnection uc = (URLConnection) u.openConnection();
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-type”,"application/xhtml+xml");
uc.setRequestProperty("Content-length", “” + postData.length());
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
// Read it ...
while ((sResponse = in.readLine()) != null)
System.out.println("inputLine------> “+sResponse);
in.close();
return sResponse;
}
public static String GetMD5Hash(String sInputString)
{
// Returns a hash (message digest) of the input String.
// See internet RFC 1321, “The MD5 Message-Digest Algorithm”
//Convert the String into an array of bytes.
byte[] buffer=null;
String sResult = “”;
try {
buffer = sInputString.getBytes("ASCII");
//Create the md5 hash provider.
MessageDigest md= MessageDigest.getInstance("MD5");
//Compute the hash value from the array of bytes.
md.update(buffer);
byte[] hash = md.digest();
int msb;
int lsb = 0;
int i;
// MSB maps to idx 0
final char[] hexChars ={’0’,’1’,’2’,’3’,’4’,’5’,’6’,’7’,’8’,’9’,’A’,’B’,’C’,’D’,’E’,’F’};
for (i = 0; i < hash.length; i++){
msb = ((int)hash & 0x000000FF) / 16;
lsb = ((int)hash & 0x000000FF) % 16;
sResult = sResult + hexChars[msb] + hexChars[lsb];
}
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sResult;
}
}