2011-09-28 283 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

這很好聽,我不知道。 –