2012-03-13 131 views
3

我正在學習如何使用Google Calendar API,這反過來又要求我學習如何使用DotNetOpenAuth訪問Google帳戶。我使提供的示例工作,並在控制檯程序中編寫了工作代碼以訪問和操作日曆。如何爲DotNetOpenAuth編寫VB或C#GUI客戶端?

我現在想寫一個Windows窗體應用程序(在C#或VB)做同樣的事情。我無法使OAuth2進程在GUI應用程序中工作。它編譯並運行,但不起作用。基於迄今爲止我所看到的,我已經得出結論:GetAuthorization()函數未被調用。

我已經嘗試從按鈕單擊,從構造函數和窗體Loader方法開始該過程。我已經嘗試了C#和VB。

public GoogleCal() 
{ 
    InitializeComponent(); 

    var provider = new NativeApplicationClient(
          GoogleAuthenticationServer.Description); 
    provider.ClientIdentifier = "xxxxx.apps.googleusercontent.com"; 
    provider.ClientSecret = "yyyyy"; 

    var auth = new OAuth2Authenticator<NativeApplicationClient>(
         provider, GetAuthorization); 
} 

private IAuthorizationState GetAuthorization(NativeApplicationClient arg) 
{ 
    // Get the auth URL: 
    IAuthorizationState state = new AuthorizationState(new[] { 
           CalendarService.Scopes.Calendar.GetStringValue() }); 

    state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); 
    Uri authUri = arg.RequestUserAuthorization(state); 

    // Request authorization from the user (by opening a browser window): 
    Process.Start(authUri.ToString()); 
    authCodeText = Microsoft.VisualBasic.Interaction.InputBox(
                "Paste code:", "title", ""); 

    // Retrieve the access token by using the authorization code: 
    return arg.ProcessUserAuthorization(authCodeText, state); 
} 

我明顯做錯了什麼,但我無法弄清楚它是什麼。有任何想法嗎?

+0

這是一個類似的問題,但不是我所問的。 (另一方面,它可能儘可能接近我所能得到的......)那裏討論的解決方案似乎只有.asp。我正在尋找只有c#或vb的東西。 – Brian 2012-03-13 22:05:10

+0

ASP是vb或C#你不是在找東西.net只是vb6?或者也許winforms或wpf?沒有足夠的信息來獲取所需的信息 – 2012-03-13 22:45:32

+0

我試圖創建一個Windows窗體應用程序VS 2010. 2010. – Brian 2012-03-14 00:51:13

回答

0

使用whelkaholism.blogspot的教程,我能夠做我想做的事情。我採取了一種不同的方法(部分是使用更新的OAuth2)。

我的C#表單程序代碼如下。

// Add references: 
//  DotNetOpenAuth.dll 
//  Google.Apis.dll 
//  Google.Apis.Authentication.OAth2.dll 
//  Newtonsoft.Json.Net35.dll 
// plus, add references for whichever Google App(s) you want 
//  Google.Apis.Calendar.v3.dll 

// form contains at least the following: 
//  button1: Button, start the authentication process 
//  authCode: TextBox, to recive the auth code from Google 
//  button2: Button, complete the authentication prop 
//  textBox2: TextBox, multi-line, to display status message and output 

// in addition to the libraries required for any forms program, use: 
using System.Diagnostics; 
using DotNetOpenAuth.OAuth2; 
using Google.Apis.Authentication.OAuth2; 
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; 
using Google.Apis.Calendar.v3; 
using Google.Apis.Calendar.v3.Data; 
using Google.Apis.Util; 


// methods not related to authentication deleted for space 

namespace GoogleCal 
{ 
    public partial class GoogleCal : Form 
    { 
     private static String NL = Environment.NewLine; 
     private static IAuthorizationState state; 
     private static NativeApplicationClient provider; 
     private static OAuth2Authenticator<NativeApplicationClient> auth; 
     private static CalendarService calService; 

     private void button1_Click(object sender, EventArgs e) 
     { 
      // clicked to initiate authentication process 

      // provider and state are declared above as private static 

      provider = new NativeApplicationClient(GoogleAuthenticationServer.Description); 
      provider.ClientIdentifier = "<my id>.apps.googleusercontent.com"; 
      provider.ClientSecret = "<my secret>"; 

      // next line changes if you want to access something other than Calendar 
      state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() }); 
      state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); 
      Uri authUri = provider.RequestUserAuthorization(state); 

      Process.Start(authUri.ToString()); 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      // clicked after Google code is pasted into TextBox to complete authentication process 

      // auth and calService are declared above as private static 

      auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization); 

      // create the service object to use in other methods 
      calService = new CalendarService(auth); 

      textBox2.AppendText("Ready" + NL); 
      textBox2.Update(); 
     } 

     private IAuthorizationState GetAuthorization(NativeApplicationClient arg) 
     { 
      // state is declared above as private static 
      // authCode is a TextBox on the form 
      return arg.ProcessUserAuthorization(authCode.Text, state); 
     } 
    } 
}