我正在尝试将 Google 日历集成到我的应用程序中,并且在传递 RefreshToken 的 OAuth 授权方面遇到了一些问题。我收到一个没有问题的 AccessToken,但 RefreshToken 属性为空。请参阅标有“此处错误:”的行以了解我遇到问题的位置
我的 Asp.Net MVC 控制器(名为OAuthController
)如下所示:
public ActionResult Index()
{
var client = CreateClient();
client.RequestUserAuthorization(new[] { "https://www.googleapis.com/auth/calendar" }, new Uri("http://localhost/FL.Evaluation.Web/OAuth/CallBack"));
return View();
}
public ActionResult CallBack()
{
if (string.IsNullOrEmpty(Request.QueryString["code"])) return null;
var client = CreateClient();
// Now getting a 400 Bad Request here
var state = client.ProcessUserAuthorization();
// ERROR HERE: The RefreshToken is NULL
HttpContext.Session["REFRESH_TOKEN"] = Convert.ToBase64String(Encoding.Unicode.GetBytes(state.RefreshToken));
return JavaScript("Completed!");
}
private static WebServerClient CreateClient()
{
return
new WebServerClient(
new AuthorizationServerDescription()
{
TokenEndpoint = new Uri("https://accounts.google.com/o/oauth2/token"),
AuthorizationEndpoint = new Uri("https://accounts.google.com/o/oauth2/auth"),
ProtocolVersion = ProtocolVersion.V20
}
, _GoogleClientId, _GoogleSecret);
}
我在 Google 的 API 文档中看到,我需要确保将access_type
请求设置offline
为发送 RefreshToken。如何在 Authenticator 请求中设置此值?