java - HttpUrlConnection Forbidden access to Google Shortener URL -


i have implemented class short url long url:

public class urlshortener { private final static string tag = "urlshortener"; private final static string address = "https://www.googleapis.com/urlshortener/v1/url?key=api_key";  static jsonobject jobj = null; static string json = "";  private string longurl;  public urlshortener(string longurl) {     this.longurl = longurl; }  //public jsonobject getjsonfromurl(string address,string longurl) { public jsonobject getjsonfromurl() {      url url;     httpurlconnection urlconnection = null;      // making http request     try {          //define connection           url = new url(urlshortener.address);         urlconnection = (httpurlconnection)url.openconnection();          urlconnection.setdooutput(true);         urlconnection.setdoinput(true);         urlconnection.setusecaches(false);         urlconnection.setrequestproperty("content-type", "application/json");         urlconnection.setrequestproperty("accept", "application/json");         urlconnection.setrequestmethod("post");           //define jsonobject         jsonobject datasend = new jsonobject();         log.d(tag, "longurl: " + urlencoder.encode(this.longurl, "utf-8"));         datasend.put("longurl",  urlencoder.encode(this.longurl, "utf-8"));          //send data         outputstreamwriter wr= new outputstreamwriter(urlconnection.getoutputstream());         wr.write(datasend.tostring());         wr.flush();         wr.close();          log.d(tag, "data send");         log.d(tag, "responsecode: " + string.valueof(urlconnection.getresponsecode()));          //display returns post request          stringbuilder sb = new stringbuilder();          int httpresult = urlconnection.getresponsecode();          if(httpresult == httpurlconnection.http_ok){              bufferedreader br = new bufferedreader(new inputstreamreader(urlconnection.getinputstream(),"utf-8"));              string line = null;              while ((line = br.readline()) != null) {                 sb.append(line + "\n");             }              br.close();              //system.out.println(""+sb.tostring());             log.d(tag, "sb: " + sb.tostring());          }else{             //system.out.println(urlconnection.getresponsemessage());             log.d(tag, "urlconnection.getresponsemessage(): " + urlconnection.getresponsemessage());         }          json = sb.tostring();         log.e("json", json);          // parse string json object         jobj = new jsonobject(json);       } catch (unsupportedencodingexception e) {         e.printstacktrace();         log.d(tag, "unsuppoertedencodingexception: " + e.tostring());     } catch (jsonexception e) {         e.printstacktrace();         log.d(tag, "error jsonexception: " + e.tostring());     } catch (ioexception e) {         e.printstacktrace();         log.d(tag, "ioexception: " + e.tostring());     }      // return json object     return jobj;  } 

}

when call method getjsonfromurl i've got same result responsecode = 403 (forbidden)

what doing wrong? please, me!!!

thanks in advance!!!


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -