function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
streetstreet 

Url Shortening using bitly integration in salesforce

I want use below code..

global class bitly {

Public String mode;
Public String sUrl;

public String getbitly () {
String shorten;
XmlStreamReader reader;
HttpResponse res;

//First, build the http request
Http h = new Http();
HttpRequest req = buildWebServiceRequest(sURL);

//Second, invoke web service call
if (mode=='live') { res = invokeWebService(h, req); }

if (mode=='live') { reader = res.getXmlStreamReader(); }
else
{
String str = '<bitly><results shortUrl="http://bit.ly/QqHEm">Foo bar</results></bitly>';
reader = new XmlStreamReader(str);
}
return readXMLResponse(reader,'shortUrl');

}


public static HttpRequest buildWebServiceRequest(String purl){
String endpoint;
HttpRequest req = new HttpRequest();
endpoint = 'http://api.bit.ly/shorten?version=2.0.1&format=xml&history=1&longUrl=' + purl + '&login=tseth&apiKey=R_948fa681da46221f969e83b2ba52d31e';
req.setEndpoint(endpoint);
req.setMethod('GET');
return req;
}

public static HttpResponse invokeWebService(Http h, HttpRequest req){

//Invoke Web Service
HttpResponse res = h.send(req);
return res;
}


public static String readXMLResponse(XmlStreamReader reader, String sxmltag){
string retValue;
// Read through the XML
system.debug(reader.toString());
while(reader.hasNext()) {
if (reader.getEventType() == XmlTag.START_ELEMENT) {
if (reader.getLocalName() == sxmltag) {
reader.next();
if (reader.getEventType() == XmlTag.characters) {
retValue = reader.getText();
}
}
}
reader.next();
}
return retValue;
}


}

bitly b = new bitly();
b.mode = 'live';
b.sUrl = 'http://www.bridgefarmconsulting.com';

system.debug('Check BitLy' + b.getbitly() );
Im using above code it giving error at endpoint. what to be changed their.

soofsoof

Can you post the error message here?

streetstreet

Unauthorized endpoint, please check Setup->Security->Remote site settings. endpoint = https://api.bit.ly/shorten?version=2.0.1&format=xml&history=1&longUrl=http://flourish.sandbox1.cs7.force.com&login=tseth&apiKey=R_948fa681da46221f969e83b2ba52d31e

 

An unexpected error has occurred. Your development organization has been notified.

 

 

soofsoof

The error is pretty descriptive - go to Setup > Security > Remote Site Settings and add https://api.bit.ly/ as an authorized remote URL.  Salesforce restricts callouts to all remote URLs, except those permitted via 'Remote Site Settings' page.

 

Thanks.

streetstreet

I got it.

 

thanks.