I’ve wrapped most of the google apis for calendars(events), tasks, gmail, drive etc… at least to some extent (not all completely)
I haven’t gotten around to Google+ because we haven’t needed it quite yet.
Here’s two methods i have in my GoogleApi.Google class
action()
Parameters:
action as string, url as string, content as string = “”, timeout as integer = -1
Return Jsonitem
[code] if timeout = -1 then timeout = defaultTimeout
dim hs as new HTTPSecureSocket
dim output as string
hs.SetRequestHeader(“Authorization”, TokenType + " " + Token)
hs.Secure = true
hs.Address = url
hs.SetPostContent(content, “application/json”)
dim response as string
if action = “get” then
response = hs.Get(url, defaultTimeout)
elseif action = “post” then
response = hs.Post(url, defaultTimeout)
else
response = hs.SendRequest(action, url, timeout)
end
//Check for an OAuth error - If there is, refresh token and recall query
dim js as new JSONItem(DefineEncoding(response,Encodings.UTF8))
If js.HasName(“error”) then
if js.Child(“error”).HasName(“errors”) then
if js.child(“error”).Child(“errors”).IsArray then
For i as integer = 0 to js.child(“error”).Child(“errors”).Count - 1
if js.child(“error”).Child(“errors”).Child(i).Lookup(“reason”,"") = “authError” then
//OAuth Error exists, refresh token and try again
refreshTheToken
hs.ClearRequestHeaders
hs.SetRequestHeader(“Authorization”, TokenType + " " + Token)
if action = "get" then
response = hs.Get(url, defaultTimeout)
elseif action = "post" then
response = hs.Post(url, defaultTimeout)
else
response = hs.SendRequest(action, url, timeout)
end
js = new JSONItem(DefineEncoding(response,Encodings.UTF8))
exit
end
Next
end
end
end
Return js[/code]
RefreshTheToken()
dim timeout as integer = 5
dim d as new Dictionary
d.Value("refresh_token") = RefreshToken
d.Value("client_id") = ClientId
d.Value("client_secret") = ClientSecret
d.Value("grant_type") = "refresh_token"
dim hs as new HTTPSecureSocket
dim output as string
hs.Secure = true
hs.Address = "accounts.google.com"
hs.SetFormData(d)
dim js as new JSONItem(hs.Post("accounts.google.com"+"/o/oauth2/token", timeout))
//Set Values
Token = js.Lookup("access_token","")
The other classes typically just call post methods to the Action event and then use a serialization class to help convert in and out of Jsonitems and Xojo objects
The code ends up looking pretty nice once its all wrapped:
dim response as GoogleAPI.ListMessagesResponse = g.User.Messages.List
dim attachmentCount as integer = response.messages(0).AttachmentCount
dim firstAttachment as GoogleAPI.MessageAttachment = response.messages(0).Attachment(0)