首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌OAuth2 api

谷歌OAuth2 api
EN

Stack Overflow用户
提问于 2015-02-09 20:45:38
回答 1查看 1.4K关注 0票数 0

我在设置google oauth2以访问日历api方面有问题。我使用了下面的代码,这很好,并提示用户授予对日历api的访问权限。但是,一旦用户允许访问该站点,该站点将进入一个result.Credentials重定向循环中,在调试时,似乎始终为null。使用fiddler,我可以看到从以下网址接收到一个令牌: accounts.google.com/o/oauth2/token

答复如下:

代码语言:javascript
复制
{
  "access_token" : "TOKEN",
  "token_type" : "Bearer",
  "expires_in" : 3600
}

我完全搞不懂为什么那些凭据从来不被填充。下面是我使用的代码:

代码语言:javascript
复制
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);
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-10 20:28:28

我设法弄明白了。我错过了令牌的存储方法。特别是这一行:

代码语言:javascript
复制
            DataStore = new FileDataStore("Drive.Api.Auth.Store")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28418998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档