Oppure

Loading
17/03/09 12:09
ingnuccio
In pratica sto cercando di realizzare un programmino in C# che si collega con google Calendar, ed ho un problema a fare l'autenticazione. Il mio codice è il seguente:

WebRequest request = WebRequest.Create("https://www.google.com/accounts/ClientLogin");
request.Method = "POST";
String dataPost = "<form Email=miaemail Passwd=miapassword source=exampleCo-exampleApp-1 service=cl>";
byte[] byteArray = Encoding.UTF8.GetBytes(dataPost);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();


Ora il problema credo sia nella stringa dataPost, a mio parere sbaglio a scriverla. Nella guida mi dice la seguente:

The POST body should contain a set of query parameters, as described in the following table. They should look like parameters passed by an HTML form, using the application/x-www-form-urlencoded content type.
Parameter Description
Email The user's email address.
Passwd The user's password.
source Identifies your client application. Should take the form companyName-applicationName-versionID; below, we'll use the name exampleCo-exampleApp-1.
service The string cl, which is the service name for Google Calendar.

Grazie per qualsiasi aiuto
aaa
17/03/09 15:37
ingnuccio
sono riuscito a risolvere il problema e riesco a fare il login.

Ora però ho il problema che per le successive richieste che vado a fare dovrei utilizzare il parametro stringa auth che mi ritorna il sito quando faccio il login.

Come faccio a mettere come header nelle mie richieste questo parametro?

Questo è l'header in questione

Authorization: GoogleLogin auth=yourAuthToken

yourAuthToken è la stringa di ritorno dal login che già ho
aaa
18/03/09 10:22
Il Totem
C'è la proprietà HttpWebRequest.Headers, che contiene gli headers della richiesta. Usa Add per aggiungerne uno.
aaa
18/03/09 12:04
ingnuccio
Postato originariamente da Il Totem:

C'è la proprietà HttpWebRequest.Headers, che contiene gli headers della richiesta. Usa Add per aggiungerne uno.


bene sono riuscito a risolvere in questo modo

request2.Headers.Add("Authorization", "GoogleLogin auth="+authToken);

Cosi ho aggiunto nel header l'autorizzazione. Ora però mi accorgo che non mi fa accedere perchè fa un reiderect, ecco il codice

WebRequest request2 = WebRequest.Create("google.com/calendar/feeds/default/owncalendars/…;);

request2.Headers.Add("Authorization", "GoogleLogin auth="+authToken);

request2.Method = "GET";

request.ContentType = "application/x-www-form-urlencoded";

((HttpWebRequest)request2).AllowAutoRedirect = false; // con questo pezzo di codice mi accorgo del reiderect

WebResponse response2 = request2.GetResponse();

Questo è l'output che mi tira fuori

Moved Temporarily
<HTML>
<HEAD>
<TITLE>Moved Temporarily</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Moved Temporarily</H1>
The document has moved <A HREF="google.com/calendar/feeds/default/owncalendars/…;.
</BODY>
</HTML>

Come faccio ad evitare il reiderect, o come faccio a gestirlo?
aaa