我将google calendar API集成到aps.net mvc应用程序中。 我收到错误消息“访问令牌已过期,无法刷新。错误:刷新错误,刷新错误,刷新错误”。 该代码在localhost上运行时正常工作,但是在服务器上运行时出现上述错误 代码是
public async Task< ActionResult> Index()
{
var userId = User.Identity.GetUserId();
var _User = "Usr" + userId;
string credPath = Server.MapPath($"~/UsersToken/{_User}");
UserCredential UsrCred = null;
FileDataStore fileStore = new FileDataStore(credPath);
TokenResponse tokenResponse = await fileStore.GetAsync<TokenResponse>(userId);
if (!Directory.Exists(credPath))
{
Directory.CreateDirectory(credPath);
}
DirectoryInfo info = new DirectoryInfo(credPath);
DirectorySecurity security = info.GetAccessControl();
security.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
info.SetAccessControl(security);
if (tokenResponse == null)
{
var result = new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(CancellationToken.None);
if (result.Result.Credential == null) // <<- it's always true, so it always gets redirected
return Redirect(result.Result.RedirectUri);
else
{
UsrCred = result.Result.Credential;
}
}
else
{
IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = _clientId,
ClientSecret = _clientSecret
},
Scopes = Scopes,
DataStore = new FileDataStore(credPath, true)
//DataStore = new FileDataStore(Google.Apis.Auth)
});
UsrCred = new UserCredential(flow, userId, tokenResponse);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = UsrCred,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
Events events = request.Execute();
List<EventModel> Result = new List<EventModel>();
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
Result.Add(
new EventModel()
{
Sumarry = eventItem.Summary,
Description = eventItem.Description,
StartDate = eventItem.Start.DateTime,
EndDate = eventItem.End.DateTime,
GoogleMeetLink = eventItem.HangoutLink
}
);
}
}
else
{
//Console.WriteLine("No upcoming events found.");
}
return View(Result);
}
redirect_url方法是
public async Task<ActionResult> IndexAsyc(string state, string code, string scope)
{
string Lang = ViewBag.Lang ?? "ar";
string RedirectURL = HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + $"/{Lang}/Meetings/IndexAsyc";
var userId = User.Identity.GetUserId();
var _User = "Usr" + userId;
string credPath = Server.MapPath($"~/UsersToken/{_User}");
UserCredential UsrCred = null;
FileDataStore fileStore = new FileDataStore(credPath);
TokenResponse tokenResponse = await fileStore.GetAsync<TokenResponse>(userId);
// create authorization code flow with clientSecrets
GoogleAuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
DataStore = new FileDataStore(credPath),
ClientSecrets = new ClientSecrets()
{
ClientId = _clientId,
ClientSecret = _clientSecret
},
Scopes = Scopes
});
//var result = new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).AuthorizeAsync(CancellationToken.None);
if (tokenResponse == null)
{
// token data does not exist for this user
tokenResponse = await authorizationCodeFlow.ExchangeCodeForTokenAsync(
userId, // user for tracking the userId on our backend system
code,
RedirectURL, // redirect_uri can not be empty. Must be one of the redirects url listed in your project in the api console
CancellationToken.None);
}
TokenResponse tokenResponse2 = await fileStore.GetAsync<TokenResponse>(userId);
if (tokenResponse2 != null)
{
return RedirectToAction("Index");
}
var Creds = new UserCredential(authorizationCodeFlow, userId, tokenResponse2);
if (Creds != null) // result.Credential != null )
{
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = Creds,//result.Credential,
ApplicationName = ApplicationName,
});
return RedirectToAction("Index");
}
else
{
return new RedirectResult(RedirectURL);
}
}
如何设置令牌永不过期?