【问题标题】:OAuth 1.0 with Android 4.x and scribe带有 Android 4.x 和 scribe 的 OAuth 1.0
【发布时间】:2013-04-21 16:29:21
【问题描述】:

我想开发一个使用 LinkedIn API 的 Android 应用(冰淇淋三明治)。为此,我使用 Scribe 库来实现 OAuth 流程。

我的问题是,在用户允许我的 LinkedIn 应用访问他的 LinkedIn 数据后,我不知道如何从 Web 视图中获取访问令牌。

我在网上找到了很多教程,但没有教程解释如何使用冰淇淋三明治获取令牌。我在网上看到的是,我无法在带有冰淇淋三明治的 UI 线程中创建 http 调用。因此我开发了一个异步任务来获取授权 url。

在我的活动中,我有一个具有以下 OnClickListener 的按钮:

private OnClickListener createOnClickListener(final SocialAPI socialAPI) {
    return new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if(PreferencesManager.getToken(AccountsActivity.this, socialAPI) == null) {
                new OAuthRequestTokenAsyncTask(AccountsActivity.this, new AsyncTaskResultHandler<String>() {
                    @Override
                    public void handleResult(String result) {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(result)));
                    }

                    @Override
                    public void onError(Exception e) {
                        //nothing to do here
                    }
                }).execute(socialAPI);
            }
        }
    };
}

异步任务执行以下操作:

protected String doInBackground(SocialAPI... socialAPIs) {
    SocialAPI socialAPI = socialAPIs[0];

    OAuthService oauthService = new ServiceBuilder()
        .provider(socialAPI.apiClass)
        .apiKey(socialAPI.consumerKey)
        .apiSecret(socialAPI.consumerSecret)
        .callback(socialAPI.callbackUrl)
        .build();

    Token requestToken = oauthService.getRequestToken();
    return oauthService.getAuthorizationUrl(requestToken);
}

在用户在 web 视图中输入他的凭据后,原始活动再次被回调操作 noNewIntent 调用:

public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    final Uri uri = intent.getData();
    System.out.println("what to do here");
}

在这个位置,我不知道如何获取访问令牌。我想我必须开发第二个异步任务,我必须在其中注入请求令牌(根据抄写员文档),但是如何从操作 onNewIntent 中做到这一点......

Verifier verifier = new Verifier("verifier");
Token accessToken = service.getAccessToken(requestToken, verifier);

顺便说一句,如果应用程序在 UI 线程中执行 http 调用,则会出现以下异常:

org.scribe.exceptions.OAuthConnectionException: There was a problem while creating a connection to the remote service.

提前谢谢...

【问题讨论】:

  • 听起来您缺少 Internet 权限?
  • 我已经在manifest文件中设置了权限...

标签: android api oauth linkedin scribe


【解决方案1】:

是的,必须开发第二个异步任务...

public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    final Uri uri = intent.getData();
    final SocialAPI socialAPI = SocialAPI.fromScheme(uri.getScheme(), uri.getSchemeSpecificPart());

    new OAuthAccessTokenAsyncTask(this, new AsyncTaskResultHandler<Token>() {
        @Override
        public void handleResult(Token result) {
            PreferencesManager.setAccessToken(AccountsActivity.this, socialAPI, result);
        }

        @Override
        public void onError(Exception e) {
            //Nothing to do here
        }
    }, uri).execute(socialAPI);
}

【讨论】:

    猜你喜欢
    • 2012-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    相关资源
    最近更新 更多