Android 上的 Signpost OAuth:如何在两次连续运行之间重新创建 Consumer?

发布于 2024-09-26 23:06:45 字数 228 浏览 0 评论 0原文

我成功地使用 Signpost 通过 OAuth 授权调用 Google 帐户中的受保护资源。

然而,用户每次都必须通过 Google 并授予访问权限,这似乎有点奇怪。有没有一种方法可以序列化 Consumer 并重新创建它,以便不需要重新授权?我尝试将令牌、秘密和验证程序保留在共享首选项中,并将它们设置在消费者中,但我收到了 OAuthExpectationFailedException。

有什么想法吗?

I'm succesfully using Signpost to authorize calls to protected resources in a Google account via OAuth.

However it seems a bit weird that the user has to go each and every time through Google and grant access. Is there a way of serializing the Consumer and recreating it so that re-authorization is not needed? I've tried keeping the tokens, secret and verifier in the shared preferences and setting them in the Consumer but I receive a OAuthExpectationFailedException.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿看小鸭子会跑 2024-10-03 23:06:45

收到访问令牌后,您可以将其存储在您的应用程序首选项中,如下所示:

provider.retrieveAccessToken(consumer, oauth_verifier);

final Editor edit = prefs.edit();
edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
edit.commit();

之后,您可以随时重新创建消费者,如下所示:

private OAuthConsumer getConsumer(SharedPreferences prefs) {
    String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
    String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
    OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
    consumer.setTokenWithSecret(token, secret);
    return consumer;
}

一旦获得消费者,您就可以进行 API 调用,消费者将签署他们。

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
consumer.sign(request);
HttpResponse response = httpclient.execute(request);

根据signpost 文档

Signpost 对象非常轻量级,因此建议您为应用程序中必须发送签名 HTTP 请求的每个线程创建 OAuthConsumer 和 OAuthProvider。这两个对象也是可序列化的,因此您可以保留它们并在以后恢复它们。

Once you've received the access token, you can store it in your apps preferences like this :

provider.retrieveAccessToken(consumer, oauth_verifier);

final Editor edit = prefs.edit();
edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
edit.commit();

Afterwards, you can always re-create the consumer like this :

private OAuthConsumer getConsumer(SharedPreferences prefs) {
    String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
    String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
    OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
    consumer.setTokenWithSecret(token, secret);
    return consumer;
}

Once you've got the consumer, you can make API calls and the consumer will sign them.

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
consumer.sign(request);
HttpResponse response = httpclient.execute(request);

According to the signpost docs :

Signpost objects are very lightweight, so you are adviced to create an OAuthConsumer and OAuthProvider for every thread in your application that must send signed HTTP requests. Both objects are also serializable, so you can persist and restore them later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文