如何从AuthSub迁移到Google OAuth?

时间:2010-11-18 12:23:03

标签: c# .net asp.net oauth

我有一个显示Google Analytics数据的现有应用程序。目前,它存储了我不喜欢的用户名和密码,因此我想将其转换为使用OAuth。我已经隔离了身份验证方法以获取令牌,希望我所要做的就是更改此方法:

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];
}

有没有简单的方法将其转换为OAuth?我可以更改参数,但我希望我不必对我的应用程序的其余部分进行体系结构更改。谢谢!

1 个答案:

答案 0 :(得分:0)

您应该能够使用http://blog.stevienova.com/2008/04/19/oauth-getting-started-with-oauth-in-c-net/中的指南作为编写代码来获取OAuth令牌的基础。显然,您应该使用https://www.google.com/accounts/OAuthGetRequestToken(如所示here)而不是http://term.ie/oauth/example/request_token.php。我认为您不需要大幅改变您的架构才能实现这一目标。此外,您需要先授权令牌才能使用它。我认为通过http://code.google.com/apis/accounts/docs/OAuth_ref.html阅读可以为您提供所需的大部分内容。