3

I have a Google script which takes address in the url parameters and returns the latitude and longitude. It is deployed as a web app. Link :

https://script.google.com/macros/s/AKfycbxV70DhVKCPppaGC6AKN8yarNUCN54VVBY7yLyCE5lxeICfB77y/exec?Times%20Square&New%20York&NY

I have a trigger on Listings__c which takes the address of the listing (when inserted/updated), passes it to a future method in an apex class. The class then calls the Google Web App and passes it the address in the url parameters doing http request and suppose to get back the latitude and longitude in the response.

My problem is the http request does not return anything back. I am not sure if I am doing the call to the web app correctly.

This is a sample method to show the way I am calling the web app:

public class LocationClass() {

@future (callout=true)
public static void getLocation() {
    String address = 'Times Square, New York, NY';
    //CONNSTRUCT AN HTTP REQUEST
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://script.google.com/macros/s/AKfycbxV70DhVKCPppaGC6AKN8yarNUCN54VVBY7yLyCE5lxeICfB77y/exec?'+EncodingUtil.urlEncode(address, 'UTF-8'));
    req.setMethod('GET');
    req.setTimeout(6000);

    try {
        HttpResponse res = http.send(req);
        System.debug('OOOOOOOOO response : ' + res.getBody());
    } catch(Exception e) {}
}
}

This is a sample method, you can remove the annotation and try to run it by doing LocationClass.getLocation();

Thanks.

6
  • I am getting this back... <HTML> <HEAD> <TITLE>Moved Temporarily</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Moved Temporarily</H1> The document has moved <A HREF="google.com/accounts/…>. </BODY> </HTML> Commented Sep 19, 2013 at 23:59
  • HTTP Status 302, en.wikipedia.org/wiki/HTTP_302 Commented Sep 20, 2013 at 0:00
  • The URL returned in the Location header from this 302 response, takes me to the Google login page. Which indicates your need to login first to Google, is there a setting on your Google Script that makes it work for anonymous access without the user being logged into the Google service at all (as is the case from Apex here)? Commented Sep 20, 2013 at 0:07
  • I've sorted it, that was fun! I always say answering questions on StackExchange grows your knowledge, this was no exception, thanks! :) Commented Sep 20, 2013 at 0:58
  • I have published the app and given permissions to everyone. Commented Sep 20, 2013 at 13:13

1 Answer 1

8

I wrote this test Google Script...

enter image description here

Then made sure when I published it I selected this option so it can be accessed anonymously, without requiring Google authentication...

enter image description here

enter image description here

Google redirects the response from the published URL to a content serving URL. The Apex HTTP client does not automatically follow redirects, so you have to respond to the HTTP 302 yourself.

Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://script.google.com/macros/s/AKfycbyScn6cvxIEqg9kLy_pEWfCB43ZFNRDnWVOvc7_ucsoV7IuGYY/exec');
req.setMethod('GET');
req.setTimeout(6000);
HttpResponse res = http.send(req);
if(res.getStatusCode() == 302)
{
   req.setEndpoint(res.getHeader('Location'));
   res = http.send(req);
   System.debug('GoogleScript response : ' + res.getBody());
}

This gives the following in the Debug log.

01:47:15.683 (683951000)|USER_DEBUG|[11]|DEBUG|GoogleScript response : Hello World

Hope this helps!

P.S. I needed to add the following to the Remote Site settings in Salesforce (under Setup)

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .