我正在尝试从 Withings Api 获取用户信息,我已经使用 Scribe 库(Java)通过 Oauth 成功登录 Withings。但是当我按照 Withings Api 文档发送获取用户信息的请求时出现问题,它总是返回带有错误代码的结果。
我尝试了一些方法,但没有奏效。有人可以帮我解决这个问题。
Withings API http://www.withings.com/en/api#documentation
首先,我在 WithingsController.groovy 中调用 withings 操作以获取身份验证。
在验证成功服务器返回访问令牌后,在 withingsCallback 操作中我获取用户信息。
获取用户信息时返回的结果是Withings Api的结果码
{“状态”:2554}
这是我的代码
WithingsService.groovy
def getAuthDetails(callbackUrl) {
if (!authService) {
authService = new ServiceBuilder()
.provider(WithingsApi.class)
.apiKey( grailsApplication.config.oauth.withings.key as String )
.apiSecret( grailsApplication.config.oauth.withings.secret as String )
.callback( callbackUrl as String )
.build();
}
Token requestToken = authService.getRequestToken();
[ authUrl : authService.getAuthorizationUrl(requestToken), requestToken : requestToken ]
}
def getWithingsUserInformation(Token accessToken,String userId){
String url = 'http://wbsapi.withings.net/user?action=getbyuserid&userid='+userId;
OAuthRequest request = new OAuthRequest( Verb.POST, url )
authService.signRequest(accessToken, request)
Response response = request.send()
return response
}
def getAccessToken( params, requestToken ){
requestToken = requestToken as Token
Verifier verifier = new Verifier( params.oauth_verifier )
authService.getAccessToken(requestToken, verifier);
}
WithingsController.groovy
def withings() {
def authInfo = withingsService.getAuthDetails(createLink(action: 'withingsCallback', controller: 'withings', absolute: 'true'))
if (authInfo.requestToken)
{
session["withings_requestToken"] = authInfo.requestToken
}
}
def withingsCallback(){
def accessToken = withingsService.getAccessToken(params, session["withings_requestToken"])
session["withings_accessToken"] = accessToken
if(accessToken) {
def profile
String userId = params.userid
profile = withingsService.getWithingsUserInformation(accessToken,userId)
}
}