也许不是最好的方法,但这对我有用。据我了解,Magento 令牌不会过期。不过,它们可以手动撤销。Scribe(在其他答案中提到)是我能找到的最简单的获得授权的方法。我还使用了一个非常方便的库 Xstream,它带有一个小的 FileUtil 类,用于在 xml 中存储和检索令牌和服务对象,这样我就不必每次都授权和获取新令牌。也许不是使用的最佳实践和一些不必要的代码,但它只需要一次授权。首先,我使用 createService(),然后使用 Authorization 类的 start() 方法。以下是使用的 Java 类:
MagentoApi.java
package somepackage;
import org.scribe.builder.api.DefaultApi10a;
import org.scribe.model.Token;
public class MagentoApi extends DefaultApi10a {
private static final String AUTHORIZE_URL = "http://domain.com/admin/oauth_authorize";
@Override
public String getAccessTokenEndpoint() {
return "http://domain.com/oauth/token";
}
@Override
public String getRequestTokenEndpoint() {
return "http://domain.com/oauth/initiate";
}
@Override
public String getAuthorizationUrl(Token requestToken) {
return AUTHORIZE_URL+"?oauth_token="+requestToken.getToken();
}
}
授权.java
package somepackage;
import com.thoughtworks.xstream.XStream;
import java.io.File;
import java.util.Scanner;
import org.scribe.builder.ServiceBuilder;
import org.scribe.model.Token;
import org.scribe.model.Verifier;
import org.scribe.oauth.OAuthService;
public class Authorization {
private OAuthService service;
private Token requestToken;
private Token accessToken;
private String authUrl;
public Token start() {
File f = new File("token.xml");
if (f.exists()) {
System.out.println("Feching Existing Token");
loadToken();
return accessToken;
}
Scanner in = new Scanner(System.in);
System.out.println("--- Magento OAuth Authorization ---\n");
System.out.println("Fetching The Request Token...");
requestToken = service.getRequestToken();
System.out.println("Got The Request Token!\n");
System.out.println(" Go & Authorize Magento Here:");
authUrl = service.getAuthorizationUrl(requestToken);
System.out.println(authUrl);
System.out.println("\nPaste The Verifier Here:");
System.out.print(">> ");
Verifier verifier = new Verifier(in.nextLine());
System.out.println("\nTrading The Request Token For An Access Token...");
accessToken = service.getAccessToken(requestToken, verifier);
System.out.println("Got the Access Token!");
saveToken();
return accessToken;
}
public OAuthService createService() {
File f = new File("service.xml");
if (f.exists()) {
System.out.println("Feching Existing Service");
loadService();
return service;
}
service = new ServiceBuilder()
.provider(MagentoApi.class)
.apiKey("Consumer Key")
.apiSecret("Consumer Secret")
.build();
saveService()
return service;
}
public void saveService() {
File file = new File("service.xml");
XStream xstream = new XStream();
xstream.alias("service", OAuthService.class);
String xml = xstream.toXML(service);
try {
FileUtil.saveFile(xml, file);
} catch (Exception e) {
}
}
public void saveToken() {
File file = new File("token.xml");
XStream xstream = new XStream();
xstream.alias("token", Token.class);
String xml = xstream.toXML(accessToken);
try {
FileUtil.saveFile(xml, file);
} catch (Exception e) {
}
}
@SuppressWarnings("unchecked")
public void loadService() {
File file = new File("service.xml");
XStream xstream = new XStream();
xstream.alias("service", OAuthService.class);
try {
String xml = FileUtil.readFile(file);
service = (OAuthService) xstream.fromXML(xml);
} catch (Exception e) {
}
}
@SuppressWarnings("unchecked")
public void loadToken() {
File file = new File("token.xml");
XStream xstream = new XStream();
xstream.alias("token", Token.class);
try {
String xml = FileUtil.readFile(file);
accessToken = (Token) xstream.fromXML(xml);
} catch (Exception e) {
}
}
}
FileUtil.Java
package somepackage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
public class FileUtil {
private static final Charset CHARSET = Charset.forName("UTF-8");
public static String readFile(File file) throws IOException {
StringBuilder stringBuffer = new StringBuilder();
BufferedReader reader = Files.newBufferedReader(file.toPath(), CHARSET);
String line = null;
while ((line = reader.readLine()) != null) {
stringBuffer.append(line);
}
reader.close();
return stringBuffer.toString();
}
public static void saveFile(String content, File file) throws IOException {
BufferedWriter writer = Files.newBufferedWriter(file.toPath(), CHARSET);
writer.write(content, 0, content.length());
writer.close();
}
}