J'ai une application existante qui affiche des données Google Analytics. Actuellement, il stocke le nom d'utilisateur et le mot de passe que je n'aime pas, donc je voulais le convertir pour utiliser OAuth. J'ai isolé la méthode d'authentification pour obtenir le jeton dans l'espoir que tout ce que je dois faire est de changer cette méthode:Comment migrer vers Google OAuth depuis AuthSub?
public static string getSessionTokenClientLogin(string email, string password)
{
//Google analytics requires certain variables to be POSTed
string postData = "Email=" + email + "&Passwd=" + password;
//defined - should not channge much
postData = postData + "&accountType=HOSTED_OR_GOOGLE" + "&service=analytics" + "&source=testcomp-testapp-1";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Stream responseBody = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(responseBody, encode);
//returned from Google Analytics API
string response = readStream.ReadToEnd();
//get the data we need
string[] auth = response.Split(new string[] { "Auth=" }, StringSplitOptions.None);
//return it (the authorization token)
return auth[1];
}
est-il un moyen facile de convertir en OAuth? Je peux changer les paramètres, mais j'espère ne pas avoir à apporter de changements architecturaux au reste de mon application. Merci!
Salut TruMan, l'avez-vous réellement fait fonctionner? Pouvez-vous poster votre code quelque part s'il vous plaît? – Burjua