c# - 如何为 DotNetOpenAuth 编写 VB 或 C# GUI 客户端?

标签 c# vb.net winforms user-interface dotnetopenauth

我正在学习如何使用 Google Calendar API,这反过来要求我学习如何使用 DotNetOpenAuth 访问 Google 帐户。我使提供的示例正常工作,并在控制台程序中编写了工作代码以访问和操作日历。

我现在想编写一个 Windows 窗体应用程序(用 C# 或 VB)来做同样的事情。我无法使 OAuth2 进程在 GUI 应用程序中工作。它编译并运行,但不起作用。根据目前所见,我得出的结论是未调用 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);
}

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

最佳答案

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

关于c# - 如何为 DotNetOpenAuth 编写 VB 或 C# GUI 客户端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9691141/

相关文章:

c# - 将 Outlook 电子邮件内容发送到 asp.net 页面

c# - 如何编码 .NET 中的结构以供 native 代码使用?

mysql - 如何检查 MySQL 数据库中的重复名称?

c# - 尝试循环访问 Form 上的 Button 控件时出现 'Unable to cast object of type' 错误

c# - 为什么foreach调用GetHashCode?

c# - 从 VS 2010 C# XML 文档生成 HTML/帮助文件

mysql - CommandText 属性尚未正确初始化

winforms - 如何将 app.config 移动到解决方案资源管理器中的其他文件夹?

c# - 同时播放两种声音

c# - 运行多个 SqlCommand 和 ExecuteNonQuery