2013-06-12 20 views
1

我想从Withings Api获取用户信息,我已经成功地使用Scribe库(Java)以Oauth登录Withings。但是,当我发送请求获取用户信息后跟Withings Api文档时,总是返回带有错误代码的结果,这时会出现问题。如何从Withings api获取用户信息grails

我试过一些方法,但没有奏效。有人可以帮助我解决这个问题。

Withings阿比http://www.withings.com/en/api#documentation

首先我打电话withings行动WithingsController.groovy得到验证。

验证成功服务器返回访问令牌后,在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) 
     } 
    } 
+0

您能告诉我们更多信息吗?在典型的Oauth流程中,您1)获得auth url 2)将用户重定向到auth url 3)用户通过accesstoken认证和提供者调用您的服务器回调4)您使用访问令牌。您能否向我们展示#3,更具体地说,您在回调中所做的事情,最后得到的错误是什么。 – ikumen

+0

我已经添加了更多细节。希望你能帮助我解决这个问题 – user2476726

回答

1

除非我遗漏了一些东西,否则看起来你没有重定向你的用户来获得“访问令牌”。你会得到一个请求令牌后:

  • 你然后生成一个身份验证URL
  • 将用户重定向到该认证网址
  • 如果认证成功,他们将验证
  • ,提供者将调用你的回调访问令牌

所以你withings行动应包括:

def withings() { 
    def authInfo = withingsService.getAuthDetails(createLink(action: .... 

    if (authInfo.requestToken) 
    { 
    session["withings_requestToken"] = authInfo.requestToken 
    } 

    //are you missing this? 
    redirect(authInfo.authUrl) 
} 

如果您使用某种类型的http调试/日志记录,请在withings操作后检查以下请求。

https://oauth.withings.com/account/authorize? 
oauth_callback=http%3A%2F%2Fexample.com%2Fget_access_token 
&oauth_consumer_key=c331c571585e7c518c78656f41582e96fc1c2b926cf77648223dd76424b52b 
&oauth_nonce=369f9ceb2f285ac637c9a7e9e98019bd 
&oauth_signature=OR9J9iEl%2F2yGOXP2wk5c2%2BWtYvU%3D 
&oauth_signature_method=HMAC-SHA1 
&oauth_timestamp=1311778988 
&oauth_token=5bb105d2292ff43ec9c0f633fee9033045ed4643e9871b80ce586dc1bf945 
&oauth_version=1.0 
+0

感谢您的支持。 – user2476726

+0

你也可以测试获取用户信息的情况下,因为我已经登录成功,但我无法获得用户数据。 – user2476726

+0

@ user2264997您有权访问用户详细信息?请指导我在这我挣扎像任何东西 –