【问题标题】:Fitbit for Android, automatically grab user data without OAuth each time I start appFitbit for Android,每次启动应用程序时自动抓取用户数据,无需 OAuth
【发布时间】:2014-08-26 01:02:15
【问题描述】:

我有以下将 FitBit 集成到 Android 的代码,它是从这个库 https://github.com/manishsri01/FitbitIntegration 中使用的,我可以获取 response.getBody() 在 webview 中显示 JSON 正文,但我希望应用程序能够自动每次运行应用程序时,无需登录并获取 OAuth 的 PIN 码即可更新代码。我能做些什么来解决这个问题?我还想将 JSON .getBody() 解析为单独的字符串变量。我怎样才能做到这一点?

MainActivity

public class MainActivity extends Activity {

OAuthService service;
Token requestToken;
// Replace these with your own api key and secret
private String apiKey = "************************";
private String apiSecret = "*************************";

private String accessToken;
private String tokenSecret;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final WebView wvAuthorize = (WebView) findViewById(R.id.wvAuthorize);
    final EditText etPIN = (EditText) findViewById(R.id.etPIN);

    service = new ServiceBuilder().provider(FitbitApi.class).apiKey(apiKey)
            .apiSecret(apiSecret).build();

    // network operation shouldn't run on main thread
    new Thread(new Runnable() {
        public void run() {
            requestToken = service.getRequestToken();
            final String authURL = service
                    .getAuthorizationUrl(requestToken);

            // Webview nagivation should run on main thread again...
            wvAuthorize.post(new Runnable() {
                @Override
                public void run() {
                    wvAuthorize.loadUrl(authURL);
                }
            });
        }
    }).start();

}

public void btnRetrieveData(View view) {
    EditText etPIN = (EditText) findViewById(R.id.etPIN);
    String gotPIN = etPIN.getText().toString();

    final Verifier v = new Verifier(gotPIN);

    // network operation shouldn't run on main thread
    new Thread(new Runnable() {
        public void run() {
            Token accessToken = service.getAccessToken(requestToken, v);

            OAuthRequest request = new OAuthRequest(Verb.GET,
                    "http://api.fitbit.com/1/user/-/profile.json");
            service.signRequest(accessToken, request); // the access token from step
            // 4
            final Response response = request.send();
            final TextView tvOutput = (TextView) findViewById(R.id.tvOutput);

            // Visual output should run on main thread again...
            tvOutput.post(new Runnable() {
                @Override
                public void run() {
                    tvOutput.setText(response.getBody());
                }
            });
        }
    }).start();
}
}

FitBitApi

public class FitbitApi extends DefaultApi10a {

private static final String AUTHORIZE_URL = "https://www.fitbit.com/oauth/authorize?oauth_token=%s";

public String getAccessTokenEndpoint() {
    return "https://api.fitbit.com/oauth/access_token";
}

public String getRequestTokenEndpoint() {
    return "https://api.fitbit.com/oauth/request_token";
}

public String getAuthorizationUrl(Token token) {
    return String.format(AUTHORIZE_URL, token.getToken());
}

}

【问题讨论】:

  • 我快速检查了他们的 api,但没有看到刷新令牌端点。如果您阅读 OAuth,您会发现这是另一种选择,而不是每次都进行完整的身份验证。不过,他们可能不会在这一轮中提供那种端点。例如,这里是 google 的 developers.google.com/accounts/docs/OAuth2WebServer#refresh
  • 他们的 API 文档真的很糟糕,我上周一直在努力解决这个问题
  • 看起来我会回到这个例子来展示一个彻底的答案:)
  • 嗨@AndyRoid 你能指导我一下吗?我可以使用 Chrome 自定义选项卡通过 fitbit 登录。但是现在,我想从 fitbit 获取/获取用户活动的数据。我该怎么做?

标签: java android json oauth fitbit


【解决方案1】:

听起来您在这里有两个不同的问题。首先,关于保存凭据,有多种方法可以做到这一点,最简单的可能是将用户/pin 详细信息保存在 Android 的 SharedPreferences 中。但是,您仍然需要请求访问令牌。您还应该保存访问令牌(在缓存或数据库中)并重新使用它,直到它过期。如果这些凭据被认为是私有的,您可能需要了解保护这些凭据的方法。

您关于解析 JSON 的第二个问题很常见,如果您打算将 JSON 对象映射到 Java 对象,您应该考虑使用 Google 的 GSON 或 Jackson JSON 处理器。

如果您打算让 Fitbit 的 API 成为您应用程序的大部分,请考虑使用 Spring Social 并创建一个 Fitbit 端点(有一个 Spring for Android 使用 Spring Social)。虽然这可能有点矫枉过正,但我​​通常喜欢这种结构。

【讨论】:

  • 你能提供一些示例代码吗?我对 OAuth 和 JSON 的 exp 非常少
猜你喜欢
  • 1970-01-01
  • 2012-09-11
  • 1970-01-01
  • 2012-07-16
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多