如何编写一个使用DotNetOpenAuth的VB或C# GUI客户端?

3
我正在学习如何使用Google Calendar API,这需要我学习如何使用DotNetOpenAuth来访问Google帐户。我已经使提供的示例正常工作,并编写了控制台程序中的工作代码以访问和操作日历。
现在我想编写一个Windows窗体应用程序(使用C#或VB)来实现同样的功能。我无法在GUI应用程序中使OAuth2过程正常工作。它可以编译和运行,但并不起作用。根据我目前看到的,我得出结论GetAuthorization()函数没有被调用。
我尝试从按钮单击、构造函数和表单装载方法开始进程。我尝试了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);
}

我显然做错了什么,但我无法弄清楚是什么。有任何想法吗?

那是一个类似的问题,但并非我所问的。 另一方面,这可能是我能得到的最接近的答案... 在那里讨论的解决方案似乎仅适用于.asp。 我正在寻找只有c#或vb的东西。 - Brian
ASP是使用VB或C#开发的,您不是在找.NET框架下的VB6吗?或者可能是WinForms或WPF?目前提供的信息不足以了解您所需的内容。 - Micah Armantrout
我正在尝试创建一个Windows Form应用程序,使用VS 2010。 - Brian
Windows窗体应用程序(使用C#或VB) - Micah Armantrout
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接