2011-09-28 172 views
3

我正在维护一些我想添加聊天功能的代码。我已经有一个客户端/服务器/数据库设置,但我想要做的是使用Google帐户登录,而不是人们必须创建新帐户等。 任何人都可以指向正确的方向吗? 我已经下载了这个http://code.google.com/p/google-gdata/我在正确的轨道上吗?C#谷歌登录

只是为了澄清,这不是一个asp项目,它是一个桌面项目。

回答

3

经过大量的修补,我能够想出这个解决方案。它看起来很容易,但我无法找到任何适当的文件。我相信人们会发现这非常有帮助。 刚刚从http://code.google.com/p/google-gdata/ 得到谷歌的API,并添加Google.GData.Client.dll作为参考(这是Redist文件夹下)

using Google.GData.Client; 
public bool Google_Login(string email,string password, string captcha,string captchatoken) 
{ 
    String AppName = "MyCompany-MyApp-1.0.0.0"; 
    Service s = new Service("code", AppName); //Can use any service, I just happened to want to use GoogleCode 
    s.setUserCredentials(email, password); 
    if(captcha != null && captchatoken != null) //If we already tried to log in, but we needed to captcha 
    { 
     if(!String.IsNullOrWhiteSpace(captcha) || !String.IsNullOrWhiteSpace(captchatoken)) 
     { 
      s.Credentials.CaptchaToken = captchatoken; 
      s.Credentials.CaptchaAnswer = captcha; 
     } 
    } 
    try 
    { 
     string ret = s.QueryClientLoginToken(); 
     //If we end up here, then the credentials are correct. 
    } 
    catch(Google.GData.Client.CaptchaRequiredException ce) 
    { 
     //ce.Url is the full url of the Captcha image 
     //If you would rather handle the captcha from 
     //a webpage, you can make your user go here "https://www.google.com/accounts/DisplayUnlockCaptcha" 

     //TODO Some way to display the captcha image and get user feedback 
     //we'll just say you did that and returned a string named retCaptcha 
     return Google_Login(email,password, retCaptcha,ce.Token); 
    } 
    catch(Google.GData.Client.AuthenticationException re) 
    { 
     //re.Message is the specific message google sends back about the login attempt. 
     return false; 
    } 
    catch(WebException e) 
    { 
     //Haven't ended up here yet, but I'm sure it's possible. 
     return false; 
    } 
    //If we somehow end up here 
    return false; 
} 

这不是我用确切的代码,我只是拉骨架并以更通用的方式重写它。

0

对于桌面应用程序,请查看有关OAuth 1.0 for Installed Applications的Google文档。您的应用程序(显然)需要启动浏览器的功能,以便用户可以使用Google进行身份验证。

+0

其实它不需要启动浏览器。 –

+0

这很好听,我不知道。 –