Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java自动登录Magento OAuth_Java_Magento_Rest_Oauth - Fatal编程技术网

使用Java自动登录Magento OAuth

使用Java自动登录Magento OAuth,java,magento,rest,oauth,Java,Magento,Rest,Oauth,我正在尝试自动登录Magento的其余服务。一切正常。除非我总是需要手动输入授权URL。我正试图找到一个服务或其他东西,我可以把授权的URL,并得到验证码从它回来。不需要手动操作 我使用了以下代码/教程: 谢谢 编辑:现在您需要手动执行授权步骤,您需要单击按钮“批准”,并需要登录到Magento的后端。我想自动完成这项工作。因此,授权步骤和后端登录需要自动进行我相信您正在寻找类似的内容: 您应该对oauth_令牌和oauth_令牌保密。 如果您使用的是java,您可以查看scribe: Ma

我正在尝试自动登录Magento的其余服务。一切正常。除非我总是需要手动输入授权URL。我正试图找到一个服务或其他东西,我可以把授权的URL,并得到验证码从它回来。不需要手动操作

我使用了以下代码/教程:

谢谢


编辑:现在您需要手动执行授权步骤,您需要单击按钮“批准”,并需要登录到Magento的后端。我想自动完成这项工作。因此,授权步骤和后端登录需要自动进行

我相信您正在寻找类似的内容:

您应该对oauth_令牌和oauth_令牌保密。 如果您使用的是java,您可以查看scribe:

Magento拉取请求:

Magento连接:

我相信您正在寻找这样的产品:

您应该对oauth_令牌和oauth_令牌保密。 如果您使用的是java,您可以查看scribe:

Magento拉取请求:

Magento连接:

也许不是最好的方式,但这对我很有效。 据我所知,Magento代币不会过期。不过,它们可以手动撤销。 抄写(在另一个答案中提到)是我能找到的获得授权的最简单的方法。 我还使用了一个非常方便的库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();
}

}
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) {
    }
}

}
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();
}
}

也许不是最好的方式,但这对我来说很有效。 据我所知,Magento代币不会过期。不过,它们可以手动撤销。 抄写(在另一个答案中提到)是我能找到的获得授权的最简单的方法。 我还使用了一个非常方便的库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();
}

}
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) {
    }
}

}
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();
}
}

我不确定我是否完全理解你的问题-请详细说明。编辑了Zaske的第一篇帖子我不确定我是否完全理解你的问题-请详细说明。编辑了Zaske的第一篇帖子如果我要将这些代码部署到WAR文件并发送到生产服务器,我将如何保持验证代码首次通过Magento验证?我应该将验证程序代码粘贴到token.xml或service.xml的哪个节点?如果我要将这些代码部署到WAR文件并发送到生产服务器,我将如何保持验证程序代码首次通过Magento验证?我应该将验证程序代码粘贴到token.xml或service.xml的哪个节点?