How to get value from HTML tag stored in a variable?
-
11-12-2019 - |
Question
I want to fetch calenders details of all users in a domain. I am trying to do this by using UrlFetchApp and send get request to server.
var urlFetch = UrlFetchApp.fetch(url, fetchArgs);
I passed all required parameters for this. But its gives 302 Moved Temporarily server error. Sometimes its work but not always. To remove this problem, i have to send again a get request to server with same content and parameters. When send first get request its return a url, a session id is appended to it. so i need to send a request with this new url.
I used the following code for this:
function getCalender(){
var users = UserManager.getAllUsers();
var scope = 'https://www.google.com/calendar/feeds/';
var fetchArgs = googleOAuth_('calender', scope);
for(i=0;i<users.length;i++ ){
var url ='https://www.google.com/calendar/feeds/'+users[i].getEmail()+'/private/full';
try{
var urlFetch=UrlFetchApp.fetch(url,fetchArgs)
}
catch(err){
Logger.log(err)
}
}
}
function googleOAuth_(name,scope) {
var oAuthConfig = UrlFetchApp.addOAuthService(name);
oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oAuthConfig.setConsumerKey("");
oAuthConfig.setConsumerSecret("");
return {oAuthServiceName:name,
oAuthUseToken:"always",
content:'application/atom+xml',
method:"GET",
Authorization:'GoogleLogin auth=?'};
}
When i print err value its gives:
Exception: Request failed for returned code 302. Server response: <HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="https://www.google.com/calendar/feeds/<email-id>/private/full?gsessionid=y1oQCjGH3LqTKJgyh9BlhA">here</A>.
</BODY>
</HTML>
I want to get this moved id to send a new request with new url. Please give me any idea to solve this problem !
Solution
This would be done by converting server response in a string, then split the string to get special text from the paragraph.