已设法连接到xero API并获取请求令牌。但是在重新启动项目(用于调试)之后,它主要在请求访问令牌时出现以下错误
The current TokenRepository doesn't have a current request token
根据我的理解和观察,错误是随机提出的。即有时它显示,有时它不会。相关代码如下:
public class HomeController : Controller
{
IOAuthSession oauthSession = ServiceProvider.GetCurrentSession();
//
// GET: /Home/
public ActionResult XeroConnect()
{
Session.Clear();
Repository repository = ServiceProvider.GetCurrentRepository();
oauthSession.MessageLogger = new DebugMessageLogger();
// Can we already access an organisation??
if (oauthSession.HasValidAccessToken && repository != null && repository.Organisation != null)
{
return new RedirectResult("~/");
}
if (oauthSession is XeroApiPrivateSession)
{
throw new ApplicationException("The current private session cannot access the authorised organisation. Has the access token been revoked?");
}
var callbackUri = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port, "Home/Callback");
RequestToken requestToken = oauthSession.GetRequestToken(callbackUri.Uri);
string authorisationUrl = oauthSession.GetUserAuthorizationUrl();
return new RedirectResult(authorisationUrl);
}
public ActionResult Callback()
{
string verificationCode = Request.Params["oauth_verifier"];
AccessToken accessToken = oauthSession.ExchangeRequestTokenForAccessToken(verificationCode); // error shows here
GetAndStoreAuthorisedOrganisationName();
Repository repository = ServiceProvider.GetCurrentRepository();
List<XeroApi.Model.Invoice> invoices = (from invoice in repository.Invoices
where invoice.LineItems.Count > 0
select invoice).ToList();
return View(invoices);
}
}
任何帮助表示赞赏。我也在要求新的请求令牌之前清除会话。