Voilà un petit code permettant de montrer l'utilisation de l'API Google. Il permet d'ajouter un nouvel évènement à son calendrier distant.
Code : Tout sélectionner
; - Google Calendar
; - Auteur : Cls
; - Version : Juillet 2008
;
; Ce code fait appel à l'API Google afin d'ajouter un événement à son calendrier
; Vous devez posséder un compte Google valide (mail + pass)
;
#INTERNET_OPEN_TYPE_DIRECT = 1
#HTTP_ADDREQ_FLAG_ADD = $20000000
#HTTP_ADDREQ_FLAG_REPLACE = $80000000
#INTERNET_FLAG_SECURE = $800000
#SECURITY_FLAG_IGNORE_UNKNOWN_CA = $100
#INTERNET_SERVICE_HTTP = 3
#INTERNET_DEFAULT_HTTP_PORT = 80
#INTERNET_DEFAULT_HTTPS_PORT = 443
#HTTP_QUERY_COOKIE = 44
; API Google : http://code.google.com/apis/calendar/developers_guide_protocol.html#AuthClientLogin
; http://code.google.com/apis/gdata/auth.html
; Renvoi le Auth
Procedure.s GoogleLogin(gUsername.s, gPassword.s)
; URL à demander
hote.s = "www.google.com"
get_url.s = "/accounts/ClientLogin"
google_result.s = ""
; CONNEXION HTTP et création de la requete
open_handle = InternetOpen_("User Agent", #INTERNET_OPEN_TYPE_DIRECT, "" , "",0)
Debug open_handle
connect_handle = InternetConnect_(open_handle, hote, #INTERNET_DEFAULT_HTTPS_PORT, "", "", #INTERNET_SERVICE_HTTP, 0, 0)
Debug connect_handle
request_handle = HttpOpenRequest_(connect_handle, "POST", get_url, "", "", 0, #INTERNET_FLAG_SECURE, 0)
Debug request_handle
headers.s = "Content-Type: application/x-www-form-urlencoded" + Chr(13) + Chr(10)
HttpAddRequestHeaders_(request_handle,headers, Len(headers), #HTTP_ADDREQ_FLAG_REPLACE | #HTTP_ADDREQ_FLAG_ADD)
;Données du formulaire à envoyer
gUsername = ReplaceString(gUsername, "@", "%40") ; Remplacement du @
post_data.s = "Email=" + gUsername
post_data + "&Passwd=" + gPassword
post_data + "&source=GCW-APP-01"
post_data + "&service=cl"
post_data_len = Len(post_data)
; Envoi de la requete
send_handle = HttpSendRequest_(request_handle, "", 0, post_data, post_data_len)
Debug send_handle
Buffer.s = Space(1024)
bytes_read.l
total_read.l = 0
; Lecture du flux descendant
; google_result contiendra la reponse du serveur
Repeat
InternetReadFile_(request_handle, @Buffer, 1024, @bytes_read)
google_result + Left(Buffer, bytes_read)
Buffer = Space(1024)
total_read + bytes_read
Until bytes_read=0
Debug "total_read : " + Str(total_read)
Debug "GC " + google_result
; Traite le réponse pour n'afficher que le auth qui nous intéresse
i_w.l = 1
Repeat
line.s = StringField(google_result, i_w, Chr(10))
;Debug line
f1.s = StringField(line, 1, "=")
f2.s = StringField(line, 2, "=")
If UCase(f1) = "AUTH"
ProcedureReturn f2
EndIf
i_w + 1
Until line = ""
ProcedureReturn ""
EndProcedure
; http://code.google.com/apis/calendar/developers_guide_protocol.htm
; Pourquoi ca ne marcherait pas : http://www.google.com/support/calendar/bin/answer.py?hl=en&answer=36589
Procedure.s CreateEventXML(title.s, content.s, where.s, startDay.s, startTime.s, endDay.s, endTime.s)
xml.s = "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'>"
xml + "<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'></category>"
xml + "<title type='text'>" + title + "</title>"
xml + "<content type='text'>" + content + "</content>"
xml + "<gd:transparency value='http://schemas.google.com/g/2005#event.opaque'>"
xml + "</gd:transparency>"
xml + "<gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'>"
xml + "</gd:eventStatus>"
xml + "<gd:where valueString='" + where + "'></gd:where>"
xml + "<gd:when startTime='" + startDay + "T" + startTime + ".000Z' endTime='" + endDay + "T" + endTime + ".000Z'>"
xml + "</gd:when>"
xml + "</entry>"
ProcedureReturn xml
EndProcedure
Procedure.l SendGoogleEvent(Auth.s, xml.s)
header.s = "Host: www.google.com" + #CRLF$
header + "MIME-Version: 1.0" + #CRLF$
header + "Accept: text/xml" + #CRLF$
header + "Authorization: GoogleLogin auth=" + Auth + #CRLF$
;header + "Content-length: " + Str(Len(xml)) + #CRLF$
header + "Content-type: application/atom+xml" + #CRLF$
header + "Cache-Control: no-cache" + #CRLF$
header + "Connection: close " + #CRLF$ + #CRLF$
; URL à demander
host.s = "www.google.com"
get_url.s = "/calendar/feeds/default/private/full"
google_result.s = ""
; CONNEXION HTTP et création de la requete
open_handle = InternetOpen_("User Agent", #INTERNET_OPEN_TYPE_DIRECT, "" , "",0)
connect_handle = InternetConnect_(open_handle, host, #INTERNET_DEFAULT_HTTPS_PORT, "", "", #INTERNET_SERVICE_HTTP, 0, 0)
request_handle = HttpOpenRequest_(connect_handle, "POST", get_url, "", "", 0, #INTERNET_FLAG_SECURE, 0)
HttpAddRequestHeaders_(request_handle, header, Len(header), #HTTP_ADDREQ_FLAG_REPLACE | #HTTP_ADDREQ_FLAG_ADD)
;Données du formulaire à envoyer
;username = ReplaceString(username, "@", "%40") ; Remplacement du @
post_data.s = xml
;Debug post_data
post_data_len = Len(post_data)
; Envoi de la requete
send_handle = HttpSendRequest_(request_handle, "", 0, post_data, post_data_len)
Buffer.s = Space(1024)
bytes_read.l
total_read.l
total_read = 0
; Lecture du flux descendant
; google_result contiendra la reponse du serveur
Repeat
InternetReadFile_(request_handle, @Buffer, 1024, @bytes_read)
google_result + Left(Buffer,bytes_read)
Buffer = Space(1024)
Until bytes_read=0
Debug google_result
If FindString(google_result, "Error 401", 1) > 1
ProcedureReturn #False
Else
ProcedureReturn #True
EndIf
EndProcedure
; S'authetifie auprès du service Google et renvoi le hash de session (Auth)
auth.s = GoogleLogin("votremail@heberg.com", "motdepasse")
Debug "Google AUTH : " + auth
now = date()
;
;
endNow = Date() + 600
;
startDay.s = FormatDate("%yyyy-%mm-%dd", now)
startTime.s = FormatDate("%hh:%ii:%ss", now)
endDay.s = FormatDate("%yyyy-%mm-%dd", endNow)
endTime.s = FormatDate("%hh:%ii:%ss", endNow)
xmlEvent.s = CreateEventXML("Un rendez - vous", "", "", startDay, startTime, endDay, endTime)
Debug xmlEvent
;
;
;
;
;
SendGoogleEvent(auth, xmlEvent)