【问题标题】:Magento 1.9 REST API Scribe-Java Request Token FailedMagento 1.9 REST API Scribe-Java 请求令牌失败
【发布时间】:2015-04-21 21:27:58
【问题描述】:

尝试使用 REST API 和 Scribe-Java 1.3.7 连接 Magento 1.9

如下创建 Web 服务角色和消费者:

之后,运行以下代码:

public final class MagentoThreeLeggedOAuth extends DefaultApi10a {

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

    @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...
    }

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

    public static class Main {

        public static void main(String[] args) {
            final String MAGENTO_API_KEY = "[xxxxxxxxx]";
            final String MAGENTO_API_SECRET = "[yyyyyyyyyy]";
            final String MAGENTO_REST_API_URL = "http://my.magentoshop.com/api/rest";

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

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

        }

    }

}

以下是控制台上显示的错误:

Magento's OAuth Workflow

Fetching the Request Token...
obtaining request token from http://my.magentoshop.com/oauth/token
setting oauth_callback to oob
generating signature...
using base64 encoder: CommonsCodec
base string is: POST&http%3A%2F%2Fmy.magentosgop.com%2Foauth%2Ftoken&oauth_callback%3Doob%26oauth_consumer_key%3Dxxxxxxxxxxxxxxxx%26oauth_nonce%3D2939370741%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1429517137%26oauth_version%3D1.0
signature is: nINxAFwv6woAAQYbdHn6v2Uc+lw=
appended additional OAuth parameters: { oauth_nonce -> 2939370741 , oauth_signature -> nINxAFwv6woAAQYbdHn6v2Uc+lw= , oauth_callback -> oob , oauth_consumer_key -> 20482d9e12ead3420a4c5aeb6978bf8e , oauth_timestamp -> 1429517137 , oauth_signature_method -> HMAC-SHA1 , oauth_version -> 1.0 }
using Http Header signature
sending request...
response status code: 400
response body: oauth_problem=parameter_absent&oauth_parameters_absent=oauth_token
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_token'
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41)
    at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:27)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:64)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:40)
    at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:45)
    at MagentoThreeLeggedOAuth$Main.main(MagentoThreeLeggedOAuth.java:49)

我错过了什么或做错了什么?

谢谢

【问题讨论】:

    标签: java rest magento oauth


    【解决方案1】:

    好的,刚刚发现错误是:

    改变这个:

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

    到这里:

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

    【讨论】:

      猜你喜欢
      • 2021-12-23
      • 2014-11-27
      • 1970-01-01
      • 1970-01-01
      • 2015-05-15
      • 2017-06-23
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      相关资源
      最近更新 更多