基于网络套件令牌的身份验证

时间:2018-07-11 13:13:32

标签: netsuite

我正在尝试通过基于令牌的身份验证netsuite 我在这里尝试过,但是给出错误

{"error" : {"code" : "INVALID_LOGIN_ATTEMPT", "message" : "Invalid login attempt."}}

我引用了堆栈解决方案中给出的代码

Oauth implementation in netsuite using php

我现在将数据字符串传递为”,因为我只想进行身份验证。 代码中可能缺少什么?可以在没有脚本ID和部署ID的情况下运行此代码吗?

2 个答案:

答案 0 :(得分:1)

否,没有脚本和部署,您将无法运行代码。并且数据字符串应该是一个对象,即使它只是诸如“ {“ test”:true}'之类的样本数据。

此外,您所引用的PHP从技术上来说是不完整的。为了将数据正确发送到NetSuite的RESTLets,由于并发限制,您需要实现自动重试。就是说,它对解决连接问题没有帮助。

您还应该调用NetSuite数据中心以获取正确的URL。

 * NetSuite SuiteAnser for datacenter calls
 * https://netsuite.custhelp.com/app/answers/detail/a_id/65684
 * 
 * Sample production response:
 * {"webservicesDomain":"https://webservices.na1.netsuite.com","restDomain":"https://rest.na1.netsuite.com","systemDomain":"https://system.na1.netsuite.com"}

答案 1 :(得分:0)

我遇到了同样的错误。这是我发生的,因为我用于身份验证的签名生成方法不正确。您可以从Netsuite登录审核跟踪中看到特定的登录尝试错误。以下是我编写的Java代码,用于生成正确的身份验证标头,并且对我有用。

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.text.Document;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.security.GeneralSecurityException;

public class Main {

    private static final String ALPHA_NUMERIC_STRING = "2312312312312sadadadadNKSNSKMSLMXSX";
    static String OAuth ="null";


    public static void main(String[] args) {

        try {

            String oauth_val = mediate();

            URL url = new URL("https://rest.na1.netsuite.com/app/site/hosting/restlet.nl?script=xxx&deploy=x");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Authorization",oauth_val);
            conn.setRequestProperty("Content-Type","application/json");

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (conn.getInputStream())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }



    public static String randomAlphaNumeric(int count) {
        StringBuilder builder = new StringBuilder();
        while (count-- != 0) {
            int character = (int)(Math.random()*ALPHA_NUMERIC_STRING.length());
            builder.append(ALPHA_NUMERIC_STRING.charAt(character));
        }
        return builder.toString();
    }

    private static String computeSignature(String baseString, String keyString) throws GeneralSecurityException, UnsupportedEncodingException {

        final String EMPTY_STRING = "";
        final String CARRIAGE_RETURN = "\r\n";
        final String UTF8 = "UTF-8";
        String HMAC_SHA1 = "HmacSHA1";

        SecretKeySpec key = new SecretKeySpec(keyString.getBytes(UTF8), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(key);
        byte[] bytes = mac.doFinal(baseString.getBytes(UTF8));
        String base= bytesToBase64String(bytes).replace(CARRIAGE_RETURN, EMPTY_STRING);
        return URLEncoder.encode(base, "UTF-8");

    }

    private static String bytesToBase64String(byte[] bytes) {
        return Base64Encoder.getInstance().encode(bytes);
    }

    public static String mediate() {
        try {
            String BASE_URL =  "https://rest.na1.netsuite.com/app/site/hosting/restlet.nl";
            String HTTP_METHOD =  "GET";
            String TOKEN_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            String TOKEN_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            String CONSUMER_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            String CONSUMER_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            String SIGNATURE_METHOD = "HMAC-SHA1";
            String OAUTH_NONCE = randomAlphaNumeric(20);
            String TIME_STAMP = String.valueOf(System.currentTimeMillis() / 1000);
            String OAUTH_VERSION = "1.0";
            String SCRIPT_DEPLOYMENT_ID = "xxx";
            String SCRIPT_ID = "xxx";
            String REALM= "xxxxx";
            Document xmldoc = null;




            String data = "";
            data = data + "deploy=" + SCRIPT_DEPLOYMENT_ID + "&";
            data = data + "oauth_consumer_key=" + CONSUMER_KEY + "&";
            data = data + "oauth_nonce=" + OAUTH_NONCE + "&";
            data = data + "oauth_signature_method=" + SIGNATURE_METHOD +"&";
            data = data + "oauth_timestamp=" + TIME_STAMP + "&";
            data = data + "oauth_token=" + TOKEN_ID + "&";
            data = data + "oauth_version=" + OAUTH_VERSION + "&";
            data = data + "script=" + SCRIPT_ID;
            String encodedData = encode(data);

            System.out.println("This is the Encoded Data.... : "+ encodedData);

            String completeData = HTTP_METHOD + "&" + encode(BASE_URL) + "&"+ encodedData;

            System.out.println("This is the completeData.... : "+ completeData);

            String key ="";
            key = encode(CONSUMER_SECRET) + "&" + encode(TOKEN_SECRET);

            System.out.println("This is the constructed key.... : "+ key);
            String signature= computeSignature(completeData,key);


            OAuth = "OAuth realm=\"" + REALM + "\",";
            OAuth = OAuth + "oauth_consumer_key=\""+ CONSUMER_KEY + "\",";
            OAuth = OAuth + "oauth_token=\"" + TOKEN_ID + "\",";
            OAuth = OAuth + "oauth_signature_method=\"HMAC-SHA1\",";
            OAuth = OAuth + "oauth_timestamp=\"" + TIME_STAMP + "\",";
            OAuth = OAuth + "oauth_nonce=\"" + OAUTH_NONCE + "\",";
            OAuth = OAuth + "oauth_version=\"" + "1.0" + "\",";
            OAuth = OAuth + "oauth_signature=\"" + signature + "\"";


            return OAuth;

        } catch (UnsupportedEncodingException | GeneralSecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "error";
    }

    private static String encode(String value) {
        String encoded = "";
        try {
            encoded = URLEncoder.encode(value, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String sb = "";
        char focus;
        for (int i = 0; i < encoded.length(); i++) {
            focus = encoded.charAt(i);
            if (focus == '*') {
                sb += "%2A";
            } else if (focus == '+') {
                sb += "%20";
            } else if (focus == '%' && i + 1 < encoded.length()
                    && encoded.charAt(i + 1) == '7' && encoded.charAt(i + 2) == 'E') {
                sb += '~';
                i += 2;
            } else {
                sb += focus;
            }
        }
        return sb.toString();
    }

   }