Create a POST body using Google Apps Script - post

I'm trying to create a Google Apps Script that adds a new owner to the user's Google calendar. The first code block below works correctly (returns the calendar ACL in JSON format). How can I add a new user to the acl using Google Apps Script? The second code block shows my attempt to insert a new rule into the acl.
function getCalendarACL() {
// Get Calendar ID, script user's email, and the API Key for access to Calendar API
var calId = 'abc123#group.calendar.google.com';
var userEmail = Session.getActiveUser().getEmail();
var API_KEY = '012345abc123';
// Get authorization to access the Google Calendar API
var apiName = 'calendar';
var scope = 'https://www.googleapis.com/auth/calendar';
var fetchArgs = googleOAuth_(apiName, scope);
// Get the authorization information and the given calendar
fetchArgs.method = 'GET';
// Get the requested content (the ACL for the calendar)
var base = 'https://www.googleapis.com/calendar/v3/calendars/';
var url = base + calId + '/acl?key=' + API_KEY;
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
Logger.log(content);
}
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("anonymous");
oAuthConfig.setConsumerSecret("anonymous");
return {oAuthServiceName:name, oAuthUseToken:"always"};
}
Here's the second code block that returns server error 400 ("parse error"):
function insertRule() {
// Get Calendar ID, script user's email, and the API Key for access to Calendar API
var calId = 'abc123#group.calendar.google.com';
var userEmail = Session.getActiveUser().getEmail();
var API_KEY = '012345abc123';
var newUserEmail = 'person#gmail.com';
// Get authorization to access the Google Calendar API
var apiName = 'calendar';
var scope = 'https://www.googleapis.com/auth/calendar';
var fetchArgs = googleOAuth_(apiName, scope);
// Get the authorization information and the given calendar
fetchArgs.method = 'GET';
// Create the POST request body
var rawXML = "<entry xmlns='http://www.w3.org/2005/Atom' " +
"xmlns:gAcl='http://schemas.google.com/acl/2007'>" +
"<category scheme='http://schemas.google.com/g/2005#kind'" +
"term='http://schemas.google.com/acl/2007#accessRule'/>" +
"<gAcl:scope type='user' value='"+newUserEmail+"'></gAcl:scope>" +
"<gAcl:role='writer'>" +
"</gAcl:role>" +
"</entry>";
// Get the requested content (the ACL for the calendar)
var base = 'https://www.googleapis.com/calendar/v3/calendars/';
var url = base + calId + '/acl?key=' + API_KEY;
var content = UrlFetchApp.fetch(url, fetchArgs).getContentText();
Logger.log(content);
}

I would encode the newUserEmail variable like this encodeURIComponent(newUserEmail) when making the rawXML string and then retry.

You are using:
fetchArgs.method = 'GET';
but at the Acl:insert page found here says this:
HTTP Request
POST https://www.googleapis.com/calendar/v3/calendars/calendarId/acl
So, it should be,
fetchArgs.method = 'POST';

Related

How to use LIKE operator for msg_id or from_email or to_email in query for sendgrid email activity?

var queryParams = "msg_id LIKE'pBRuJA0OSqyRAHaT2sW8hg'";
var client = new RestClient("https://api.sendgrid.com/v3/messages?query=" + queryParams + "&limit=1");
var request = new RestRequest(Method.GET);
client.Timeout = -1;
request.AddHeader("x-query-id", "{{x-query-id}}");
request.AddHeader("x-cursor", "{{x-cursor}}");
request.AddHeader("authorization", "bearer " + ApiKey);
IRestResponse response = client.Execute(request);
var sendGridEmailDetails = JsonConvert.DeserializeObject<SendGridResponse>(response.Content);
Here I want to find out msg_id start with this value 'pBRuJA0OSqyRAHaT2sW8hg' and I code for this as above. I used LIKE operator here but it gives me empty response. How to use LIKE operator if we want to find out startwith or endwith values? Does anyone know how this could be done? Thanks for your help & time.
This might be an empty result because you are not url encoding the request. It looks like you are using RestSharp to make requests to the API here, so what you can try instead is:
var params = new {
query = "msg_id LIKE 'pBRuJA0OSqyRAHaT2sW8hg'",
limit = 1
}
var client = new RestClient("https://api.sendgrid.com/v3/messages");
var request = new RestRequest(Method.GET);
request.addObject(params);
client.Timeout = -1;
request.AddHeader("x-query-id", "{{x-query-id}}");
request.AddHeader("x-cursor", "{{x-cursor}}");
request.AddHeader("authorization", "bearer " + ApiKey);
IRestResponse response = client.Execute(request);
var sendGridEmailDetails = JsonConvert.DeserializeObject<SendGridResponse>(response.Content);
This way you create an object of the parameters you want to pass and let the request object handle encoding them.

How to authenticate with twitter from a firefox plugin

Echofon abandoned their firefox twitter plugin around April 2013, but it's been maintained on github until some recent changes to the twitter API broke it.
In normal use, authentication should follow PIN-based authentication, but instead the request to https://api.twitter.com/oauth/request_token is returning "{"errors":[{"code":32,"message":"Could not authenticate you."}]}'" status='401'
I think the problem is in the TwitterClient.buildOAuthHeader function
TwitterClient.buildOAuthHeader = function (user, method, url, param)
{
var ts = Math.ceil(Date.now() / 1000);
var diff = EchofonUtils.timestampDiff();
if (diff != 0) {
EchofonUtils.debug("local timestamp " + ts + " / server timetsamp " + (ts + diff));
ts += diff;
}
var converter = Cc["#mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
var result = {};
var data = converter.convertToByteArray(user + Date.now() + url + Math.random(), result);
var ch = Cc["#mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
ch.init(ch.MD5);
ch.update(data, data.length);
var hash = ch.finish(false);
var s = convertToHexString(hash);
var oauthparam = {"oauth_consumer_key" : OAUTH_CONSUMER_KEY,
"oauth_timestamp" : ts,
"oauth_signature_method" : "HMAC-SHA1",
"oauth_nonce" : s + Math.random(),
"oauth_version" : "1.0"};
if (user.oauth_token) {
oauthparam["oauth_token"] = EchofonAccountManager.instance().get(user.user_id).oauth_token;
}
var dict = {};
for (var key in param) dict[key] = param[key];
for (var key in oauthparam) dict[key] = oauthparam[key];
var paramStr = encodeURLParameter(dict);
var base = [method, RFCEncoding(url), RFCEncoding(paramStr)].join("&");
var signature;
var secret = user.oauth_token_secret ? EchofonAccountManager.instance().get(user.user_id).oauth_token_secret : "";
var signature = EchofonSign.OAuthSignature(base, secret);
oauthparam['oauth_signature'] = signature;
var headers = [];
for (var key in oauthparam) {
headers.push(key + '="' + RFCEncoding(oauthparam[key]) + '"');
}
headers.sort();
return headers.join(",");
}
I've registered a new application at dev.twitter.com and I'm using the consumer key from that instead of the one in the repository.
Also, I've added the oauth_callback attribute to the oauthparam object, with the value set to "oob" as detailed in the PIN-based authentication link above, but the plugin is not authenticating correctly with the API.
What needs to be changed in the authorization header to correct this?
This issue has been resolved.
Instructions on how to install a patched version of the plugin here - https://github.com/echofox-team/echofon-firefox-unofficial/issues/85#issuecomment-581843812

Retrieving OAuth Verification Code via .NET HttpWebRequest

I'm attempting to replicate the OAuth steps normally done via the "Connect to QuickBooks" button using HttpWebRequest/HttpWebResponse.
It's easy at first grabbing the request token and generating the authorization link:
private const string oauthBaseUrl = "https://oauth.intuit.com/oauth/v1";
private const string urlRequestToken = "/get_request_token";
private const string urlAccessToken = "/get_access_token";
private const string verifyUrl = "https://appcenter.intuit.com";
private const string authorizeUrl = "https://appcenter.intuit.com/Connect/Begin";
...
var consumerContext = new OAuthConsumerContext
{
ConsumerKey = System.Utilities.Cryptography.Encryption.ConvertToUnsecureString(ckSS),
ConsumerSecret = System.Utilities.Cryptography.Encryption.ConvertToUnsecureString(csSS),
SignatureMethod = SignatureMethod.HmacSha1
};
IOAuthSession session = new OAuthSession(consumerContext, oauthBaseUrl + urlRequestToken, authorizeUrl, oauthBaseUrl + urlAccessToken);
IToken requestToken = session.GetRequestToken();
string authorizationLink = session.GetUserAuthorizationUrlForToken(requestToken, callbackUrl);
Then I walk through grabbing the request verification code that is generated in the set-cookie string when requesting the site at the authorization link:
var requestAuth = (HttpWebRequest) WebRequest.Create(authorizationLink);
requestAuth.Method = "GET";
requestAuth.ContentType = "application/x-www-form-urlencoded";
requestAuth.Accept = "text/html, application/xhtml+xml, */*";
requestAuth.Headers.Add("Accept-Encoding", "gzip, deflate");
requestAuth.Headers.Add("Accept-Language", "en-us");
requestAuth.Host = "appcenter.intuit.com";
requestAuth.KeepAlive = true;
var responseAuth = (HttpWebResponse) requestAuth.GetResponse();
Stream answerAuth = responseAuth.GetResponseStream();
var _answerAuth = new StreamReader(answerAuth);
string htmlAuth = _answerAuth.ReadToEnd();
// Need to grab the request verification code embedded in the set-cookie string
string cookies = responseAuth.Headers.Get("Set-Cookie");
int idx = cookies.IndexOf("__RequestVerificationToken", StringComparison.Ordinal);
if (idx > 0)
{
int startIndex = cookies.IndexOf("=", idx, StringComparison.InvariantCultureIgnoreCase);
int endIndex = cookies.IndexOf(";", startIndex + 1, StringComparison.InvariantCultureIgnoreCase);
requestVerificationCode = cookies.Substring(startIndex + 1, endIndex - (startIndex + 1));
postDataString += requestVerificationCode;
}
As I understand it, the request verification code is needed in order to get the OAuth verification code that is returned in the postdata appended to the callback URL, which is in turn needed to get the access token.
This is where the difficulty begins. Using Fiddler2, I find that the login URL for generating the OAuth verification code is https://appcenter.intuit.com/Account/LogOnJson. But no matter how much I try to replicate the HTTP POST using HttpWebRequest, all I get in return is a 500 error. I'm wondering if anyone has a working example of this step? Is this even possible? I hope so, because the alternative of pulling up IE and walking through the same steps like a macro is too ugly.
Any help on this? Thanks!
You can download the dotnet sample app for understanding how the OAUTH flow works:
https://github.com/IntuitDeveloperRelations/IPP_Sample_Code
Set your app keys in web.config.

Usage Twitter Stream api for search

i`m trying use Twitter Stream Api for searching some hashtags in Google Spreadsheet. Twitter search api useless cause i wanna trak retweet count too. My function sample here. Can anybody explain me what i must do for working well..
function miniSearch(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sumSheet = ss.getSheetByName("Readme/Settings");
// Authorize to Twitter
var oauthConfig = UrlFetchApp.addOAuthService("twitter");
oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize");
oauthConfig.setConsumerKey(TWITTER_CONSUMER_KEY);
oauthConfig.setConsumerSecret(TWITTER_CONSUMER_SECRET);
// "twitter" value must match the argument to "addOAuthService" above.
var options = {
'method': 'POST',
"oAuthServiceName" : "twitter",
"oAuthUseToken" : "always"
};
var url = "https://stream.twitter.com/1/statuses/filter.json?track="+"twitterapi";
var response = UrlFetchApp.fetch(url, options);
var tweets = JSON.parse(response.getContentText());
sumSheet.getRange('B8').setValue(tweets[0]["text"]);
}
this function return error code 504;
I don't think Google Apps Script can keep a persistent HTTP connection open which is resulting in the 504 (See Twitter Streaming APIs Doc)
[I've a basic retweet counter in this Google Spreadsheet Template (TAGS v4.0). The filterUnique formula uses this code (the pseudocode is strip out any links from tweet text then extract 1st 90% of text (to take account of any old style RT+ annotation), then if not in unique array add or add 1 to existing value):
function filterUnique(tweets){
var output = [];
var temp = {};
for (i in tweets){
if (i>0){
var tmp = tweets[i][0];
var urlPattern = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
tmp = tmp.replace(urlPattern,"")
tmp = tmp.substring(0,parseInt(tmp.length*0.9));
if (temp[tmp] == undefined){
temp[tmp] = [tweets[i][0],0];
}
temp[tmp] = [tweets[i][0],temp[tmp][1]+1];
}
}
for (i in temp){
output.push([temp[i][0],temp[i][1]]);
}
output.sort(function(a,b) {
return b[1]-a[1];
});
return output.slice(0, 12);
}
]

How to get category name in Youtube API in ASP.NET4.0 application

I have implemented You-tube API in asp.net 4.0 application.I can access all other parameter except category.
YouTubeRequestSettings settings = new YouTubeRequestSettings(ApplicationName,developerKey,myUserName,myPassword);
YouTubeRequest request = new YouTubeRequest(settings);
Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/" + YouTubeID + "");
Video objVideo = request.Retrieve<Video>(videoEntryUrl);
var desc= objVideo.Description;
var title= objVideo.Title;
var category= ? ;
How can i get category?
Thanks
OK that's it.
I found solution on my way.
var catValue = objVideo.Tags.ToList<MediaCategory>().FirstOrDefault<MediaCategory>().Value

Resources