gpt4 book ai didi

c# - Google Calendar API 返回 invalid_grant 和错误的请求

转载 作者:行者123 更新时间:2023-12-03 17:00:25 25 4
gpt4 key购买 nike

在我的开发环境中,我有一个用户,我刚刚收到了以下范围的 OAuth token 。

  • https://www.googleapis.com/auth/calendar
  • https://www.googleapis.com/auth/calendar.events
  • https://www.googleapis.com/auth/calendar.readonly

  • 一切看起来都很好,我为用户存储了 token 。然后我请求列出用户的日历,我得到了无效请求的无效请求。我使用另一个用户的 token (也在我的开发环境中)尝试相同的请求,并且它工作正常。

    我最初只有第一个范围设置,即写级别访问。这就是所有现有 token 的创建方式。在我的测试过程中,我添加了其他范围。

    我尝试在我的项目中更新 Google API 的 NuGet 包。

    这是我的类(class)正在打电话。
    public class GoogleCalendarAdapter : ICalendarAdapter {
    #region attributes
    private readonly ISiteAuthTokenQueryRepository _tokenRepo;
    private readonly GoogleCalendarSettings _settings;

    private const string APPNAME = "REDACTED";

    private const string ACL_OWNER = "owner";
    private const string ACL_WRITER = "writer";
    #endregion

    #region ctor
    public GoogleCalendarAdapter(ISiteAuthTokenQueryRepository tokenRepo,
    GoogleCalendarSettings settings) {
    _tokenRepo = tokenRepo;
    _settings = settings;
    }
    #endregion

    #region methods
    private GoogleAuthorizationCodeFlow BuildAuthorizationCodeFlow() {
    return new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer() {
    ClientSecrets = BuildClientSecrets(),
    Scopes = BuildScopeList()
    });
    }

    private CalendarService BuildCalendarService(SiteAuthToken token) {

    return new CalendarService(new BaseClientService.Initializer() {
    ApplicationName = APPNAME,
    HttpClientInitializer = BuildUserCredential(token)
    });
    }

    private ClientSecrets BuildClientSecrets() {
    return new ClientSecrets() {
    ClientId = _settings.ClientId,
    ClientSecret = _settings.ClientSecret
    };
    }

    private string[] BuildScopeList() {
    return new [] { CalendarService.Scope.Calendar };
    }

    private UserCredential BuildUserCredential(SiteAuthToken token) {
    TokenResponse responseToken = new TokenResponse() {
    AccessToken = token.AccessToken,
    RefreshToken = token.RefreshToken
    };

    return new UserCredential(BuildAuthorizationCodeFlow(), APPNAME, responseToken);
    }

    public async Task<List<Cal>> GetAllWritableCalendars(Guid siteGuid) {
    SiteAuthToken token = await GetToken(siteGuid);
    CalendarService svc = BuildCalendarService(token);

    IList<CalendarListEntry> calendars = svc.CalendarList
    .List()
    .Execute()
    .Items;

    return calendars.Where(c => c.AccessRole.Equals(ACL_OWNER, StringComparison.CurrentCultureIgnoreCase) ||
    c.AccessRole.Equals(ACL_WRITER, StringComparison.CurrentCultureIgnoreCase))
    .Select(c => new Cal() {
    Id = c.Id,
    Name = c.Summary
    })
    .OrderBy(o => o.Name)
    .ToList();
    }

    public async Task<Cal> GetCalendar(Guid siteGuid, string calendarId) {
    SiteAuthToken token = await GetToken(siteGuid);
    CalendarService svc = BuildCalendarService(token);

    CalendarListEntry entry = svc.CalendarList
    .Get(calendarId)
    .Execute();

    Cal retVal = new Cal() {
    Id = entry.Id,
    Name = entry.Summary
    };

    return retVal;
    }

    private async Task<SiteAuthToken> GetToken(Guid siteGuid) {
    SiteAuthToken retVal = await _tokenRepo.GetSiteAuthToken(siteGuid, Constants.OAUTH_PROVIDER_GOOGLE);

    if (retVal == null) {
    throw new ApplicationException($"Could not find a SiteAuthToken for specified site (SiteGuid: {siteGuid})");
    }

    return retVal;
    }

    #endregion
    }

    最佳答案

    在您描述的情况下,对我有很大帮助的是使用 Google Developer OAuth Playground .默认情况下,您可以使用 OAuthPlayground 本身作为客户端来获取授权(并观察流量)。但诀窍是进入 [设置] 齿轮并选中 [x] 使用您自己的 OAuth 凭据并尝试授权您的客户端的框。 IMO 这是一个非常有用的调试工具,我想确保您了解它。

    关于c# - Google Calendar API 返回 invalid_grant 和错误的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61690118/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com