嗨,我正在努力通过java程序在netsuite中使用OAuth1.0连接netsuite RESTAPI
但我不断收到未经授权的信息
我被困在这从过去3天,没有得到任何线索来解决这个问题。
我的程序:
package com;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang.RandomStringUtils;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
public final class Oauth1SigningInterceptor {
private static String nonce;
private static long timeStamp;
private static String tokenSignature;
private static String consumerKey = "1441e...";
private static String consumerSecret = "7a72e...";
private static String accessToken = "b6cca...";
private static String accessSecret = "4ffb0...";
public static void main(String[] args) throws Exception {
createTokenBasedAttributes();
OkHttpClient client1 = new OkHttpClient();
MediaType mediaType1 = MediaType.parse("application/json");
RequestBody body1 = RequestBody.create(mediaType1, "{\n\"q\": \"select * from role\"\n}");
Request request1 = new Request.Builder()
.url("https://TSTDRV942460.suitetalk.api.netsuite.com/services/rest/query/v1/suiteql")
.method("POST", body1).addHeader("Prefer", "transient")
.addHeader("Authorization",
"OAuth realm=\"TSTDRV942460\"," + "oauth_consumer_key=\"" + consumerKey + "\","
+ "oauth_token=\"" + accessToken + "\","
+ "oauth_signature_method=\"HMAC-SHA256\",oauth_timestamp=\"" + timeStamp + "\","
+ "oauth_nonce=\"" + nonce + "\"," + "oauth_version=\"1.0\"," + "oauth_signature=\""
+ tokenSignature + "\"")
.addHeader("Content-Type", "application/json").addHeader("Cookie", "NS_ROUTING_VERSION=LAGGING")
.build();
System.out.println(request1);
Response response1 = client1.newCall(request1).execute();
System.out.println("response1 =>" + response1);
}
private static void createTokenBasedAttributes() throws Exception {
nonce = RandomStringUtils.randomAlphanumeric(20); // unique number
timeStamp = System.currentTimeMillis() / 1000L; // current time stamp in Unix format
tokenSignature = computeSignature(nonce, timeStamp);
}
private static String computeSignature(String nonce, Long timeStamp) throws Exception {
String baseString = "TSTDRV942460" + "&" + consumerKey + "&" + accessToken + "&" + nonce + "&" + timeStamp;
String key = consumerSecret + "&" + accessSecret;
return computeSHAHash(baseString, key);
}
private static String computeSHAHash(String baseString, String key) throws Exception {
byte[] bytes = key.getBytes();
SecretKeySpec mySigningKey = new SecretKeySpec(bytes, "HmacSHA256");
try {
Mac messageAuthenticationCode = Mac.getInstance("HmacSHA256");
messageAuthenticationCode.init(mySigningKey);
byte[] hash = messageAuthenticationCode.doFinal(baseString.getBytes());
return new String(Base64.getEncoder().encode(hash));
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
throw new Exception(e);
}
}
}
从 Postman 我可以连接这个成功。我在java中添加了相同的代码,只是创建了nonce、timestamp、signature运行时。但它不起作用。。。。
暂无答案!
目前还没有任何答案,快来回答吧!