
[英]How do I fetch the text from a input in SAP GUI Desktop client using C#
[英]How do I write a VB or C# GUI client for DotNetOpenAuth?
我正在学习如何使用Google Calendar API,这反过来要求我学习如何使用DotNetOpenAuth访问Google帐户。 我使提供的示例工作,并在Console程序中编写工作代码以访问和操作日历。
我现在想要编写一个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);
}
我显然做错了什么,但我无法弄清楚它是什么。 有任何想法吗?
使用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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.