【问题标题】:Google OAuth Access Token EmptyGoogle OAuth 访问令牌为空
【发布时间】:2023-04-04 16:13:01
【问题描述】:

我认为这个问题可能会被问一百万次,但我没有找到任何答案。

我正在尝试使用我的 Unity 项目访问 Google 日历,因此我使用的是 C#,但无法使用官方的 google 客户端库,因为它不受支持。

可悲的是,每次我尝试从服务器获取访问令牌时,我都会收到 404 或只是一个空响应。

我的代码:

private static string tokenUrl = "https://oauth2.googleapis.com/";
public static string GetOAuthTokens(string auth_code, string grantType)
{
    RestClient rc = new RestClient(tokenUrl);
    RestRequest request = new RestRequest(Method.POST);
    request.Parameters.Clear();

    request.Resource = "token";
    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
    request.AddParameter("auth_code", auth_code, ParameterType.RequestBody);
    request.AddParameter("client_id", client_id, ParameterType.RequestBody);
    request.AddParameter("secret", client_secret, ParameterType.RequestBody);
    request.AddParameter("grant_type", grantType, ParameterType.RequestBody);
    request.AddParameter("redirect_uri", "http://localhost:4444", ParameterType.RequestBody);
    return rc.Execute(request).Content;
}

我已经使用以下方法调用了该方法:

 Debug.Log(OAuth2.GetOAuthTokens(code, "authorization_code"));

Debug.Log 仅用于调试。

我看到很多人说我需要将我的 Uri 列入白名单,但我无法在凭据中找到该菜单点。

我现在搜索了大约 6 个小时。请帮忙!

【问题讨论】:

  • 这也是我多年前做的一个非常简单的例子daimto.com/google-apis-auth-simple如果您有任何问题,请告诉我。我从未与 unity3d 合作过,但我很乐意帮助你解决这个问题。
  • @DalmTo 您如何使用官方文档中所述的不同端点?他们是否忘记更新文档或者您的文章是基于旧版本的?
  • @DalmTo 阅读您的文章并尝试过,但我的回复仍然是空的 + 我收到的回复代码为 0,这很奇怪
  • 自从我写完之后端点已经改变了。使用发现文档中的那个

标签: c# unity3d oauth-2.0 google-oauth


【解决方案1】:

按照here 的说明,我使用他们的 .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();

    }

关于 Unity 端代码:

public Calendar calendar = new Calendar();

private void Start()
{
    calendar.Main();
}

它给了我日历事件的夏天和日期。您可以从here下载项目。

【讨论】:

  • 感谢您的帮助,一旦我放弃,就会退回到您的 Api。但我试图让我的工作用于教育目的。
【解决方案2】:

我想通了: request.AddParameter("auth_code", auth_code, ParameterType.RequestBody);

必须是:request.AddParameter("code", auth_code, ParameterType.GetOrPost);

我认为我必须重命名参数键以匹配文档

【讨论】:

    猜你喜欢
    • 2013-05-26
    • 2017-06-09
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2017-06-16
    • 2023-03-27
    • 2014-11-20
    • 2014-03-19
    相关资源
    最近更新 更多