最善の方法ではないかもしれませんが、これは私にとってはうまくいきました。私が理解しているように、Magento トークンには有効期限がありません。ただし、手動で取り消すことができます。スクライブ(他の回答で言及)は、承認を得るために私が見つけた最も簡単な方法でした。また、XML との間でトークンとサービス オブジェクトを格納および取得するために、小さな FileUtil クラスを備えた非常に便利なライブラリ Xstream を使用して、毎回新しいトークンを承認して取得する必要がないようにします。使用されているベスト プラクティスや不要なコードではないかもしれませんが、1 回限りの承認のみが必要です。最初に 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();
}
}
Authorization.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();
}
}