2014-03-28 72 views
0

我需要你的帮助。 我需要使用REST连接到我的Magento商店,但我有一个问题来检索oauth标记。 我做了本教程中所说的内容:gmartinezgil.wordpress.com/2013/08/05/using-the-magento-rest-api-in-java-with-scribe/ 我的代码在这里可用:https://www.dropbox.com/sh/zglzl9xsxcjrpid/lGrZNYfRzG从Magento REST API检索oauth令牌

我与Magento的1.8.1.0工作使用Wampserver

这里是我的Main.java文件:

package foo; 
import java.util.Scanner; 

import org.scribe.builder.*; 
import org.scribe.builder.api.*; 
import org.scribe.model.*; 
import org.scribe.oauth.*; 
/** 
* @author jerry 
*/ 
public final class Main { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     final String MAGENTO_API_KEY = "r0ntsryd0hamwmdnjezb8joun01c4h1s"; 
     final String MAGENTO_API_SECRET = "czylgk8yxhkvrx141v1q9trx0iw4qbgt"; 
     final String MAGENTO_REST_API_URL = "http://localhost/magento/api/rest/"; 
     //final String MAGENTO_REST_API_URL = "http://localhost/magento/api.php?type=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?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(); 
    } 
} 

终端上显示:

1.0 
Magento's OAuth Workflow 

Fetching the Request Token... 
obtaining request token from http://`magentohost`/oauth/initiate/ 
setting oauth_callback to oob 
generating signature... 
using base64 encoder: CommonsCodec 
base string is: POST&http%3A%2F%2F127.0.0.1%2Fmagento%2Foauth%2Finitiate%2F&oauth_callback%3Doob%26oauth_consumer_key%3Dr0ntsryd0hamwmdnjezb8joun01c4h1s%26oauth_nonce%3D2085598405%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1395911858%26oauth_version%3D1.0 
signature is: edOifgJQfBg2QoM7ifVBWwvYj30= 
appended additional OAuth parameters: { oauth_callback -> oob , oauth_signature -> edOifgJQfBg2QoM7ifVBWwvYj30= , oauth_version -> 1.0 , oauth_nonce -> 2085598405 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> r0ntsryd0hamwmdnjezb8joun01c4h1s , oauth_timestamp -> 1395911858 } 
using Http Header signature 
sending request... 
response status code: 404 
response body: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>404 Not Found</title> 
</head><body> 
<h1>Not Found</h1> 
<p>The requested URL /magento/oauth/initiate/ was not found on this server.</p> 
</body></html> 

Exception in thread "main" org.scribe.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>404 Not Found</title> 
</head><body> 
<h1>Not Found</h1> 
<p>The requested URL /magento/oauth/initiate/ was not found on this server.</p> 
</body></html> 
' 
    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 foo.Main.main(Main.java:35) 
+0

api url返回404错误。这意味着你的api服务器不工作(http:// localhost/magento/api/rest /) – namxee

+0

仅供参考http://stackoverflow.com/questions/18483875/magento-rest-api-error-500 – shivam

+0

http:///stackoverflow.com/questions/17789938/magento-rest-api-oauth-error/19678803#19678803 – shivam

回答