【发布时间】:2019-08-04 15:37:36
【问题描述】:
我“只是”想将谷歌日历 API 集成到我的小网络项目中。用户应该能够通过 c# core (2.2) mvc 项目将日历条目添加到他的日历中。问题是我找不到任何完整的示例如何做到这一点并且尝试了很多没有任何解决方案。
主要问题 - 我如何获得许可?以及如何设置重定向网址?
为什么 google 不提供完整的 c# 核心示例?
我构建了一个简单的控制台项目(基于示例) - 如果我手动设置权限,它就可以工作。但我必须要求我的用户给予许可。
顺便说一句 - 我在https://console.developers.google.com//从https://console.developers.google.com/ 创建并保存了 ClientId、ClientSecret 等。
谢谢拉尔夫
public IActionResult GoogleCalendar(string id)
{
string refreshToken = string.Empty;
string credentialError;
var credential = GetUserCredential(out credentialError);
if (credential != null && string.IsNullOrWhiteSpace(credentialError))
{
//Save RefreshToken into Database
refreshToken = credential.Token.RefreshToken;
}
string addEventError;
string calendarEventId = string.Empty;
calendarEventId = AddCalenderEvents(refreshToken, "mytestuser@googlemail.com", "Test-Event " + DateTime.Now, DateTime.Now.AddMinutes(5), DateTime.Now.AddHours(2), out addEventError);
return View();
}
public static UserCredential GetUserCredential(out string error)
{
UserCredential credential = null;
error = string.Empty;
try
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = ClientId,
ClientSecret = ClientSecret
},
Scopes,
"mytestuser@googlemail.com",
CancellationToken.None,
new FileDataStore("Google Oauth2 Client App")).Result;
}
catch (Exception ex)
{
credential = null;
error = "Failed to UserCredential Initialization: " + ex.ToString();
}
return credential;
}
我来自谷歌
- 这是一个错误。
错误:invalid_client
未找到 OAuth 客户端。
在(不断变化的)本地端口上,我从未设置过 url。
access_type=offline
response_type=code
client_id=xxxxxx-yyyyyyyyyyyyy.apps.googleusercontent.com
redirect_uri=http://127.0.0.1:56253/authorize/
scope=https://www.googleapis.com/auth/calendar
【问题讨论】:
标签: c# asp.net-core google-api google-oauth