1

我正在尝试在 Magento 的 REST 服务中自动登录。一切正常。除了我总是需要手动输入授权 URL。我正在尝试查找服务或其他东西,我可以输入授权 URL 并从中获取验证码。无需手动操作。

我使用了这个代码/教程:http ://blog.jerrysapps.com/2012/11/05/using-the-magento-rest-api-in-java-with-scribe/

谢谢。

编辑:现在您需要手动执行授权步骤,您需要单击一个按钮来“批准”,并且需要登录到 Magento 的后端。我想自动执行此操作。所以授权和登录到后端的步骤需要自动进行

4

2 回答 2

1

我相信您正在寻找这样的东西: Magento REST api authentication

您应该保留 oauth_token 和 oauth_token_secret。如果您使用的是 java,您可以查看 scribe: https ://github.com/fernandezpablo85/scribe-java

Magento 拉取请求: https ://github.com/fernandezpablo85/scribe-java/pull/325

Magento 连接: https ://github.com/saleram1/scribe-java/commit/7fef61789236d4dff183bb35ff5ce3a5a01c153a

于 2013-02-09T15:33:53.483 回答
0

也许不是最好的方法,但这对我有用。据我了解,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();
}
}
于 2013-12-15T18:29:23.517 回答