我需要你的帮助。 我需要使用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
我正在使用Wampserver
使用Magento 1.8.1.0这是我的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 :(得分:0)
我想我找到了解决方案。我激活了mod_rewrite:http://www.webdevdoor.com/php/mod_rewrite-windows-apache-url-rewriting/并执行了http://doc.prestashop.com/display/PS16/What+you+need+to+get+started所说的内容并且它有效。