【发布时间】:2021-10-07 16:18:43
【问题描述】:
我想在 winforms 的网络浏览器控件中显示我的 Google 日历。
我尝试按照教程https://www.daimto.com/google-api-and-oath2/
但不幸的是,我仍然没有获得访问我的谷歌日历的权限,“浏览器不安全......”。
我知道我必须以某种方式使用这些权限,但我不知道如何。
代码:
public partial class Kalender : UserControl
{
public string clientId = "897492813624-glvfpm6heik1fqr7s2mjtl6eme5ad8bq.apps.googleusercontent.com";
public string redirectUri = "https://calendar.google.com/";
public string clientSecret = "HYGFBEd_BZPcWcrPopsX4uZr";
public string redirectURI = "https://calendar.google.com/";
public AuthResponse access;
public Kalender()
{
InitializeComponent();
webKalender.Navigate(AuthResponse.GetAutenticationURI(clientId, redirectUri));
}
private void webKalender_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string Mytitle = ((WebBrowser)sender).DocumentTitle.ToLower();
if (Mytitle.IndexOf("success code=") > -1)
{
webKalender.Visible = false;
// searching the body for our code
string AuthCode = webKalender.DocumentTitle.Replace("Success code=", "");
string webText = ((WebBrowser)sender).DocumentText;
int start = webText.IndexOf("id=\"code\"");
start = webText.IndexOf(AuthCode, start);
int end = webText.IndexOf('"', start);
string authCode = webText.Substring(start, end - start);
//Exchange the code for Access token and refreshtoken.
access = AuthResponse.Exchange(authCode, clientId, clientSecret, redirectURI);
//processAccess();
}
}
教程中的课程:
public class AuthResponse
{
private string access_token;
public string Access_token
{
get
{
// Access token lasts an hour if its expired we get a new one.
if (DateTime.Now.Subtract(created).Hours > 1)
{
refresh();
}
return access_token;
}
set { access_token = value; }
}
public string refresh_token { get; set; }
public string clientId { get; set; }
public string secret { get; set; }
public string expires_in { get; set; }
public DateTime created { get; set; }
//"{\n \"access_token\" : \"ya29.kwFUj-la2lATSkrqFlJXBqQjCIZiTg51GYpKt8Me8AJO5JWf0Sx6-0ZWmTpxJjrBrxNS_JzVw969LA\",\n \"token_type\" : \"Bearer\",\n \"expires_in\" : 3600,\n \"refresh_token\" : \"1/ejoPJIyBAhPHRXQ7pHLxJX2VfDBRz29hqS_i5DuC1cQ\"\n}"
public static AuthResponse get(string response)
{
AuthResponse result = (AuthResponse)JsonConvert.DeserializeObject(response);
result.created = DateTime.Now; // DateTime.Now.Add(new TimeSpan(-2, 0, 0)); //For testing force refresh.
return result;
}
public void refresh()
{
var request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
string postData = string.Format("client_id={0}&client_secret={1}&refresh_token={2}&grant_type=refresh_token", this.clientId, this.secret, this.refresh_token);
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
var refreshResponse = AuthResponse.get(responseString);
this.access_token = refreshResponse.access_token;
this.created = DateTime.Now;
}
public static AuthResponse Exchange(string authCode, string clientid, string secret, string redirectURI)
{
var request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
string postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code", authCode, clientid, secret, redirectURI);
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
var x = AuthResponse.get(responseString);
x.clientId = clientid;
x.secret = secret;
return x;
}
public static Uri GetAutenticationURI(string clientId, string redirectUri)
{
string scopes = "https://www.googleapis.com/auth/calendar";
if (string.IsNullOrEmpty(redirectUri))
{
redirectUri = "urn:ietf:wg:oauth:2.0:oob";
}
string oauth = string.Format("https://accounts.google.com/o/oauth2/auth?client_id={0}&redirect_uri={1}&scope={2}&response_type=code", clientId, redirectUri, scopes);
return new Uri(oauth);
}
}
对德国人感到抱歉,但它说的是:“浏览器或应用程序我不安全。使用不同的浏览器。”
【问题讨论】:
-
请编辑您的问题并附上您的授权码,如果没有看到您尝试过的内容,我们将无法帮助您解决哪些问题。此外,calendar.google.com 不是对 Google 日历 api 的 api 调用的正确格式。您了解 google 日历 api 是否会以 Json 格式返回数据取决于您。
-
这是您遇到的错误吗? developers.googleblog.com/2021/06/…您使用的是哪个版本的 .net 框架?
-
我添加了两张网页浏览器中正在发生的事情的图片。我在 .net 框架 4.8
-
本机默认安装的是哪个浏览器?好的,在我尝试调试这个之前,您不想使用 google .net 客户端库的任何原因?您应该可以将其与已安装的应用程序一起使用,这没有问题。我最初编写该教程是因为我使用的是库不支持的 .net 3.5。
标签: c# winforms oauth google-oauth google-calendar-api