Google OAuth2 API

奇异果

我在设置Google api oauth2以访问日历api时遇到问题。我在下面使用了以下代码,它可以正常工作,并提示用户授予对日历api的访问权限。但是,一旦用户允许访问,该站点就会在重定向循环中结束,在调试时,结果将显示为空。凭据始终为null。使用提琴手,我可以看到正在从以下网址接收令牌:account.google.com/o/oauth2/token

具有以下响应:

{
  "access_token" : "TOKEN",
  "token_type" : "Bearer",
  "expires_in" : 3600
}

我完全为为什么从来没有填充凭据而感到困惑。这是我正在使用的代码:

public class AppFlowMetadata : FlowMetadata
{
    private static readonly IAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = "CLIENT_ID",
                ClientSecret = "CLIENT_SECRET"
            },
            Scopes = new[] { CalendarService.Scope.Calendar }
        });

    public override string GetUserId(Controller controller)
    {
        // In this sample we use the session to store the user identifiers.
        // That's not the best practice, because you should have a logic to identify
        // a user. You might want to use "OpenID Connect".
        // You can read more about the protocol in the following link:
        // https://developers.google.com/accounts/docs/OAuth2Login.
        var user = controller.Session["user"];
        if (user == null)
        {
            user = Guid.NewGuid();
            controller.Session["user"] = user;
        }
        return user.ToString();

    }

    public override IAuthorizationCodeFlow Flow
    {
        get { return flow; }
    }
}




public class AuthCallbackController : Google.Apis.Auth.OAuth2.Mvc.Controllers.AuthCallbackController
{
    protected override Google.Apis.Auth.OAuth2.Mvc.FlowMetadata FlowData
    {
        get { return new AppFlowMetadata(); }
    }
}

public class GoogleController : Controller
{
    // GET: Google
    [Route("google")]
    public ActionResult Index(CancellationToken cancellationToken)
    {
        //try to get results
        var result = new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
                        AuthorizeAsync(cancellationToken).Result;


        if (result.Credential != null)
        {
            //// This bit checks if the token is out of date, 
            //// and refreshes the access token using the refresh token.
            if (result.Credential.Token.IsExpired(SystemClock.Default))
            {
                Google.Apis.Auth.OAuth2.Responses.TokenResponse token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse();
                //If the token is expired recreate the token
                token = result.Credential.Flow.RefreshTokenAsync("1", result.Credential.Token.RefreshToken, CancellationToken.None).Result;

                //Get the authorization details back
                result = new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(cancellationToken).Result;
            }
            var service = new CalendarService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = result.Credential,
                    ApplicationName = "ASP.NET MVC Sample"
                });

            return View();
        }
        else
        {
            return new RedirectResult(result.RedirectUri);
        }
    }
奇异果

我设法弄清楚了。我缺少令牌的存储方法。特别是这一行:

            DataStore = new FileDataStore("Drive.Api.Auth.Store")

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用oAuth2时缺少Google表格api密钥

无法从Google API OAuth2刷新令牌

Google OAuth2 API按子域的单独令牌

Google日历API的OAuth2登录名

您如何将 Google 服务 oAuth2 转换为 Google Ads API oAuth2 访问权限

使用LWP :: Authen :: OAuth2访问受OAuth2保护的Google API时出现问题

将Google EmailSettings API python代码从OAuth1移到OAuth2服务帐户

Google API - 使用 API 密钥而不是 OAuth2 从 Google 表格中读取

如何在 Google api 中使用 Passport.js Google OAuth2 策略?

Google API Google Drive Node.js 无法上传文件 oAuth2

使用Google Cloud Storage API和开发人员密钥(不是OAuth2)

带有Google OAuth2授权服务器的安全Spring RESTful API

Google OAuth2 Java API 在回調/重定向 URI 中設置狀態

如何在Google API中使用oauth2和刷新令牌?

Google API OAuth2,服务帐户,“错误”:“ invalid_grant”

如何不将OAuth2与Google API结合使用(尤其是adwords)

从Oauth2 Google通讯录API获取用户信息

使用邮递员使用 Google API 获取 OAuth2 令牌

Google Drive API - 无法读取未定义的属性“OAuth2”

Google Adwords API PHP-删除当前访问令牌的Oauth2访问权限

如何在Node.js中的Google OAuth2中发现API

尝试使用Google Blogger API OAuth2创建帖子时发生SessionExpiredException

如何使用OAuth2和PassportJS获取Google Fitness API数据

Google Analytics(分析)API:如何解决OAuth2缺少范围参数错误?

如何从OAuth2 Google API获取电子邮件和个人资料信息?

使用Google Calendar API oauth2服务帐户(node.js)模拟用户

Firebase-NodeJS:带有OAuth2的Google API的域范围委派

使用oauth2和Google API无法识别的参数

如何使用Javascript从Google Api检索服务帐户OAuth2令牌?