C# Google OAuth访问令牌为空
我想这个问题可能被问了一百万次,但我没有找到任何答案 我试图通过我的Unity项目访问Google日历,所以我使用C#,但不能使用官方的Google客户端库,因为它不受支持 不幸的是,每次我试图从服务器获取访问令牌时,我要么得到404,要么只是一个空响应 我的代码: 我用以下方法调用了该方法:C# Google OAuth访问令牌为空,c#,unity3d,oauth-2.0,google-oauth,C#,Unity3d,Oauth 2.0,Google Oauth,我想这个问题可能被问了一百万次,但我没有找到任何答案 我试图通过我的Unity项目访问Google日历,所以我使用C#,但不能使用官方的Google客户端库,因为它不受支持 不幸的是,每次我试图从服务器获取访问令牌时,我要么得到404,要么只是一个空响应 我的代码: 我用以下方法调用了该方法: Debug.Log(OAuth2.GetOAuthTokens(code, "authorization_code")); 那Debug.Log只是为了调试 我看到很多人说我需要将
Debug.Log(OAuth2.GetOAuthTokens(code, "authorization_code"));
那Debug.Log
只是为了调试
我看到很多人说我需要将我的Uri列入白名单,但我在凭证中找不到该菜单点
我现在搜索了大约6个小时。请帮忙 如前所述,我使用他们的.NET SDK制作了一个Google日历的DLL文件。 我对它进行了一些修改,以便与Unity一起使用,然后在Unity项目中使用了该dll文件。这对我有用 下面是DLL代码:
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
static string ApplicationName = "Google Calendar API .NET Quickstart";
public void Main()
{
UserCredential credential;
using (var stream =
new FileStream(UnityEngine.Application.streamingAssetsPath + "/credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Debug.Log("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Debug.Log("Event Summery: " + eventItem.Summary + " | " + " On " + when);
}
}
else
{
Debug.Log("No upcoming events found.");
}
Console.Read();
}
public Calendar calendar = new Calendar();
private void Start()
{
calendar.Main();
}
统一端代码:
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
static string ApplicationName = "Google Calendar API .NET Quickstart";
public void Main()
{
UserCredential credential;
using (var stream =
new FileStream(UnityEngine.Application.streamingAssetsPath + "/credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Debug.Log("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Debug.Log("Event Summery: " + eventItem.Summary + " | " + " On " + when);
}
}
else
{
Debug.Log("No upcoming events found.");
}
Console.Read();
}
public Calendar calendar = new Calendar();
private void Start()
{
calendar.Main();
}
它给我日历事件摘要和日期。您可以从下载项目。我发现了:
request.AddParameter(“auth_code”,auth_code,ParameterType.RequestBody)代码>
必须是:request.AddParameter(“code”,auth_code,ParameterType.GetOrPost)代码>
我想我必须重命名参数键以匹配文档试试这个。这也是我几年前做的一个非常简单的例子。如果你有任何问题,请告诉我。我从未与unity3d合作过,但我很乐意帮助您解决这个问题。@DalmTo为什么您要使用官方文档中所述的不同端点?他们忘记更新文档了吗?或者你的文章是基于旧版本的吗?@DalmTo阅读了你的文章并尝试了一下,但我的回复仍然是空的+我收到的回复代码是0,这很奇怪,自从我写了那篇文章以来,端点已经发生了变化。使用discovery Doc中的一个感谢您的帮助,一旦我放弃,就回到您的Api。但我想让我的工作用于教育目的。