当验证者出错时会发生什么
让我先解释一下我的场景。我使用 Scribe 通过 Oauth 连接到 Scribe 不直接支持的 API。也就是说,我要连接的服务器需要输入 4 位数字作为创建 accessToken 的最后一步。
虽然这很有效,但当用户输入错误的 4 位数字时,应用程序将永远运行,不会抛出任何错误或异常(据我所知)。这是我正在使用的代码:
//verifierString has the 4-digit number
verifier = new Verifier(verifierString);
Logger.debug("Trading the Request Token for an Access Token...");
accessToken = service.getAccessToken(requestToken, verifier);
应用程序永远保留在 getAccessToken 中。我想知道是否有办法控制这个,因为用户很可能输入错误的 4 位数字。我检查了 getAccessToken、Oauth 1.0 的代码,因为它是我正在使用的代码:
public Token getAccessToken(Token requestToken, Verifier verifier)
{
config.log("obtaining access token from " + api.getAccessTokenEndpoint());
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
config.log("setting token to: " + requestToken + " and verifier to: " + verifier);
addOAuthParams(request, requestToken);
appendSignature(request);
Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody());
}
似乎只是 send() 抛出了 RuntimeException,但不确定从我的代码中捕获它是否是正确的方法...... .有什么建议吗?
谢谢 ! 亚历克斯
Let me explain my scenario first. I'm using Scribe to connect via Oauth to an API not directly supported by Scribe. That said, the server I'm connecting to requires to enter a 4 digit number as the final step for being able to create the accessToken.
While this is working great, when the user inputs the 4 digits wrong, the app stays running forever, no error or excepction is thrown (to my understanding). This is the code I'm using:
//verifierString has the 4-digit number
verifier = new Verifier(verifierString);
Logger.debug("Trading the Request Token for an Access Token...");
accessToken = service.getAccessToken(requestToken, verifier);
App stays in getAccessToken forever. I wonder if there is a way to control this, since it's very possible that users input the 4 digit wrong. I've checked the code for getAccessToken, Oauth 1.0 since it's the one I'm using:
public Token getAccessToken(Token requestToken, Verifier verifier)
{
config.log("obtaining access token from " + api.getAccessTokenEndpoint());
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());
config.log("setting token to: " + requestToken + " and verifier to: " + verifier);
addOAuthParams(request, requestToken);
appendSignature(request);
Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody());
}
It only seems that send() throws a RuntimeException, but not sure if catching it from my code is the right way to do it....any suggestions?
Thanks !
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试任何
scribe
示例,您会发现输入错误的验证程序代码会立即导致OAuthException
(请注意,这是一个OAuthException 不过,正如您正确指出的那样,它继承自
RuntimeException
,这是设计使然,因为我 100% 反对检查异常)。问题出在服务器端,正如你所说,它使 http 连接长时间保持打开状态,甚至可能永远保持打开状态,直到发生默认连接超时。
也许如果您在 Api 论坛中提问,您会得到更好的答复,同时使用 scribe 的 debug 模式(自 1.3.0 起新增)可以帮助您诊断大多数问题。
If you try any of the
scribe
examples you'll see that entering a wrong verifier code results in an immediateOAuthException
(note that it is anOAuthException
though, as you correctly state it inherits fromRuntimeException
, this is by design since I'm 100% against checked exceptions).The problem is on the server side, for what you say it keeps the http connection open for long or possibly even forever, until the default connection timeout happens.
Perhaps if you ask in the Api forums you'll get a better response, also using scribe's debug mode (new since 1.3.0) helps you diagnose most problems.