【问题标题】:Unable to get request token from 3rd party Magento REST API无法从第 3 方 Magento REST API 获取请求令牌
【发布时间】:2015-11-06 04:11:41
【问题描述】:

我的一位客户已经设置了 Magento。现在,我正在尝试访问他们的 Magento API 来获取产品信息。他们为我提供了 Magento 主机名、consumerkey 和 consumersecret。我正在使用 Scribe 1.3.7 来访问 API。这是我的代码:

public final class MagentoThreeLeggedOAuth extends DefaultApi10a {
    private static final String BASE_URL = "http://example.com/";

    @Override
    public String getRequestTokenEndpoint() {
        return BASE_URL + "oauth/initiate";

    }

    @Override
    public String getAccessTokenEndpoint() {
        return BASE_URL + "oauth/token";
    }

    @Override
    public String getAuthorizationUrl(Token requestToken) {
        return BASE_URL + "admin/oauth_authorize?oauth_token="
                + requestToken.getToken(); //this implementation is for admin roles only...
    }
}

public final class MagentoAuth {

    /**
     * @param args
     */
    public static void main(String[] args) {
        final String MAGENTO_API_KEY = "abcdefghij";
        final String MAGENTO_API_SECRET = "qwertyuiop";
        final String MAGENTO_REST_API_URL = "http://example.com/magento/api/rest";          

        // three-legged oauth
        OAuthService service = new ServiceBuilder()
        .provider(MagentoThreeLeggedOAuth.class)
        .apiKey(MAGENTO_API_KEY)
        .apiSecret(MAGENTO_API_SECRET)
        .debug()
        .build();

        System.out.println("" + service.getVersion());
        Scanner in = new Scanner(System.in);
        System.out.println("Magento's OAuth Workflow");
        System.out.println();

        // Obtain the Request Token
        System.out.println("Fetching the Request Token...");
        Token requestToken = service.getRequestToken();
        System.out.println("Got the Request Token!");
        System.out.println();

        System.out.println("Fetching the Authorization URL...");
        String authorizationUrl = service.getAuthorizationUrl(requestToken);
        System.out.println("Got the Authorization URL!");
        System.out.println("Now go and authorize Main here:");
        System.out.println(authorizationUrl);
        System.out.println("And paste the authorization code here");
        System.out.print(">>");

        Verifier verifier = new Verifier(in.nextLine());
        System.out.println();
        System.out.println("Trading the Request Token for an Access Token...");

        Token accessToken = service.getAccessToken(requestToken, verifier);
        System.out.println("Got the Access Token!");
        System.out.println("(if your curious it looks like this: "
                + accessToken + " )");
        System.out.println();

        OAuthRequest request = new OAuthRequest(Verb.GET, MAGENTO_REST_API_URL+ "/products");
        service.signRequest(accessToken, request);

        Response response = request.send();
        System.out.println();
        System.out.println(response.getCode());
        System.out.println(response.getBody());
        System.out.println();
    }
}

当我尝试运行程序时,我收到以下错误:

1.0
Magento's OAuth Workflow

Fetching the Request Token...
obtaining request token from http://example.com/oauth/initiate
setting oauth_callback to oob
generating signature...
using base64 encoder: DatatypeConverter
base string is: POST&http%3A%2F%2Fexample.com%2Foauth%2Finitiate&oauth_callback%3Doob%26oauth_consumer_key%3Dabcdefghij%26oauth_nonce%3D2387862876%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1437522397%26oauth_version%3D1.0
signature is: 7DHYSmiU9JMnek04Pd8JwtbaeP4=
appended additional OAuth parameters: { oauth_callback -> oob , oauth_signature -> 7DHYSmiU9JMnek04Pd8JwtbaeP4= , oauth_version -> 1.0 , oauth_nonce -> 2387862876 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> abcdefghij , oauth_timestamp -> 1437522397 }
using Http Header signature
sending request...
response status code: 400
response body: oauth_problem=parameter_absent&oauth_parameters_absent=oauth_consumer_key
Exception in thread "main" org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: 'oauth_problem=parameter_absent&oauth_parameters_absent=oauth_consumer_key'

这里有什么问题?和代码有关系吗?或者我的客户必须做些什么?我在本地安装了 Magento,并且可以使用此代码获取访问令牌。我不想在没有首先了解问题所在的情况下回到我的客户那里。

谢谢。

【问题讨论】:

    标签: java magento oauth magento-1.9 scribe


    【解决方案1】:

    网址好像不正确:

     private static final String BASE_URL = "http://example.com/";
    

    而不是那个 在下面使用

     private static final String BASE_URL = "http://example.com/magento/";
    

    【讨论】:

      猜你喜欢
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 1970-01-01
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      相关资源
      最近更新 更多