如何在Android

时间:2016-11-21 13:25:44

标签: android integration basecamp

我正在使用我的Android应用程序中的basecamp OAuth 2.0集成。

https://github.com/basecamp/api/blob/master/sections/authentication.md#get-authorization

我正在按照以下文档进行实施。

获取accessstoken的库,所有内容如下。 https://github.com/dmfs/oauth2-essentials

我完成了下面的事情

// Create OAuth2 provider
OAuth2AuthorizationProvider provider = new BasicOAuth2AuthorizationProvider(
URI.create("https://launchpad.37signals.com/authorization/new"),
URI.create("https://launchpad.37signals.com/authorization/token"),
new Duration(1,0,3600)

OAuth2ClientCredentials credentials = new BasicOAuth2ClientCredentials(
"client-id //from >https://integrate.37signals.com/", "client-secret" // From >https://integrate.37signals.com/);

// Create OAuth2 client
OAuth2Client client = new BasicOAuth2Client(
provider,
credentials,
URI.create("https://example.com/sandbox97/basecampoauth/basecamploginresponse") //this is from "https://integrate.37signals.com/"/* Redirect URL */);


// Start an interactive Authorization Code Grant
OAuth2InteractiveGrant grant = new AuthorizationCodeGrant(
client, new BasicScope("scope"));


// Get the authorization URL and open it in a WebView
URI authorizationUrl = grant.authorizationUrl();

现在,当我在webview中打开上面的url时,它将返回 “https://example.com/sandbox97/basecampoauth/basecamploginresponse?code=12345678&state= ********** h9JKj_KvpVo-oiMe_5 ***************** yLeIb *******”

然后按照说明我会打电话

// Open the URL in a WebView and wait for the redirect to the redirect URL
// After the redirect, feed the URL to the grant to retrieve the access token
OAuth2AccessToken token = grant.withRedirect(redirectUrl).accessToken(executor);

喜欢我的代码

public class getToken extends AsyncTask<String, Void, String> {
    String lsUrl;

    public getToken(String fsUrl) {
        lsUrl = fsUrl;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.i(TAG, "onPreExecute called");
    }


    @Override
    protected String doInBackground(String... params) {
        HttpRequestExecutor executor = new HttpUrlConnectionExecutor();
        try {
            OAuth2AccessToken token = null;
            try {
                token = grant.withRedirect(new URI(lsUrl)).accessToken(executor);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            Log.i(TAG, "token token token token> " + token);

            s = String.valueOf(token);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ProtocolError protocolError) {
            protocolError.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "token token token token> s " + s);
        return s;
    }

    @Override
    protected void onPostExecute(String aVoid) {
        super.onPostExecute(aVoid);
        Log.i(TAG, "onPostExecute called" + aVoid);
    }
}

当我执行任务时,我将始终获得空值。

并在日志控制台中收到以下错误。

11-21 18:21:16.731 25798-25798/com.myApp I/chromium: [INFO:CONSOLE(22)] 

 "Uncaught TypeError: Cannot read property 'postMessage' of null", source:

 https://example.com/sandbox97/basecampoauth/basecamploginresponse?

 code=12345678&state=**********h9JKj_KvpVo-oiMe_5*****************yLeIb*******  (22)

我尝试了两种方式 - 使用库并根据文档使用临时。但每当我尝试从网址获取访问令牌时,我都会得到空响应。

请帮帮我。我被困2天以上。

0 个答案:

没有答案