1

当我尝试从 Java 中的 Magento 获取数据时出现签名无效问题。我的代码有什么问题:

public class MagentoFacade {

final String MAGENTO_API_KEY = "apikey";
final String MAGENTO_API_SECRET = "apisecret";
final String MAGENTO_REST_API_URL = "urlmagento/api/rest";

public void testMethod() {

    OAuthService service = new ServiceBuilder()
    .provider(MagentoThreeLeggedOAuth.class)
    .apiKey(MAGENTO_API_KEY)
    .apiSecret(MAGENTO_API_SECRET)
    .debug()
    .build();




    System.out.println("" + service.getVersion());

    // start
            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();

             // Obtain the Authorization URL
            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?limit=2");
                service.signRequest(accessToken, request);
                Response response = request.send();
                System.out.println();
                System.out.println(response.getCode());
                System.out.println(response.getBody());
                        System.out.println();
}


public static void main(String[] args) {
    MagentoFacade mf = new MagentoFacade();
    mf.testMethod();
}

}

public final class MagentoThreeLeggedOAuth extends DefaultApi10a {
private static final String BASE_URL = "urltoMagento/";

@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 + "richard/oauth_authorize?oauth_token="
            + requestToken.getToken(); //this implementation is for admin roles only...
}

}

signature is: NnRaB73FqCcFAAVB4evZtGkWE3k= appended additional OAuth parameters: { oauth_callback -> oob , oauth_signature -> NnRaB73FqCcFAAVB4evZtGkWE3k= , oauth_version -> 1.0 , oauth_nonce -> 753236685 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> ptrij1xt8tjisjb6kmdqed2v4rpla8av , oauth_timestamp -> 1359710704 } using Http Header 签名发送请求... 响应状态代码:401 响应正文:oauth_problem=signature_invalid&debug_sbs=MCe/RB8/GNuqV0qku00ubepc/Sc= 线程“主”org.scribe.exceptions.OAuthException 中的异常:响应正文不正确。无法从中提取令牌和秘密: org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41) 上的 org.scribe.

4

2 回答 2

6

我可能会为您提供答案,但它可能不适用于您的情况。我努力找出为什么我的本地机器上的签名无效。

事实证明,在 Mage_Oauth_Model_Server::_validateSignature() 中计算签名时,Magento 构建了请求 URI 部分,并修剪了 URL 端口路径$this->_request->getHttpHost()

就我而言,本地网络服务器在端口 81 上运行,因此我的签名和 Magento 的签名不匹配。通过将false参数传递给getHttpHost方法,您可以防止端口修剪。

我知道这是非常具体的,但我失去了所有的头发弄清楚为什么所以我需要分享它。谁知道呢,也许这会有所帮助。

干杯布尼

于 2014-02-04T14:44:25.200 回答
2

我只想在 Postman 中添加它,我只是添加了另一个值为 false 的 getHttpHost url 参数,并且效果也很好。我为此奋斗了一整天。我希望这可以节省其他人的时间。

于 2015-08-15T14:13:14.667 回答