invalid_grant尝试获取oAuth2令牌

时间:2018-10-26 19:32:17

标签: android oauth-2.0 nopcommerce-4.1

大家好,我正在尝试从Android的nopCommerce的nop-template API获取Oauth2访问令牌,但是在调用oAuthClient.accessToken时遇到此错误:

  

OAuthProblemException {description ='null',error ='invalid_grant',uri ='null',state ='null',scope ='null'}

我的代码是:

import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

import net.smartam.leeloo.client.OAuthClient;
import net.smartam.leeloo.client.URLConnectionClient;
import net.smartam.leeloo.client.request.OAuthClientRequest;
import net.smartam.leeloo.client.response.OAuthAccessTokenResponse;
import net.smartam.leeloo.common.exception.OAuthProblemException;
import net.smartam.leeloo.common.exception.OAuthSystemException;
import net.smartam.leeloo.common.message.types.GrantType;

import java.util.UUID;

class accessToken extends AsyncTask<Uri, Void, Void> {


    @Override
    protected Void doInBackground(Uri... uri) {

        if (uri[0] != null && uri[0].toString()
                .startsWith("myapp://Oauthresponse")) {

            String code = UUID.randomUUID().toString();

            OAuthClientRequest request = null;

            try {
                request = OAuthClientRequest.tokenLocation("http://www.myStore.com/api/token")
                        .setGrantType(GrantType.AUTHORIZATION_CODE)
                        .setClientId("ClientId")
                        .setClientSecret("ClientSecret")
                        .setRedirectURI("myapp://Oauthresponse")
                        .setCode(code)
                        .buildBodyMessage();

            } catch (OAuthSystemException e) {
                e.printStackTrace();
            }

            OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());

            OAuthAccessTokenResponse response = null;

          try {
              response = oAuthClient.accessToken(request);
            } catch (OAuthSystemException e) {
                e.printStackTrace();
                Log.d("khata", e.toString());
            } catch (OAuthProblemException e) {
                e.printStackTrace();
                Log.d("khata", e.toString());
            }
            String token = response.getAccessToken();
        }

        return null;
    }
}

有人可以帮忙吗?

0 个答案:

没有答案