tharaka prabath
10/28/2021, 3:16 AMuser
10/28/2021, 3:55 AMuser
10/28/2021, 3:55 AMuser
10/28/2021, 4:30 AMuser
10/28/2021, 4:30 AMtharaka prabath
10/28/2021, 4:31 AMuser
10/28/2021, 4:33 AMtharaka prabath
10/28/2021, 4:33 AMuser
10/28/2021, 4:42 AMuser
10/28/2021, 2:47 PMprivate String getAccessToken(String shopName, String code) {
try {
String urlParameters = "client_id="+ ShopifyClientId +"&client_secret="+ ShopifySecret +"&code=" + code;
byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
URL url = new URL("https://"+shopName+".<http://myshopify.com/admin/oauth/access_token%22|myshopify.com/admin/oauth/access_token">);
// Open a connection(?) on the URL(??) and cast the response(???)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput( true );
connection.setInstanceFollowRedirects( false );
connection.setRequestProperty( "charset", "utf-8");
connection.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
connection.setUseCaches( false );
// Now it's "open", we can set the request method, headers etc.
connection.setRequestProperty("accept", "application/json");
try( DataOutputStream wr = new DataOutputStream( connection.getOutputStream())) {
wr.write( postData );
}
String inputLine, readline = "";
// reading the HTML output of the GET HTTP request
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((inputLine = in.readLine()) != null) {
readline = readline + inputLine;
}
in.close();
return readline;
} catch (Exception e) {
log.error("Exception occurred when try to getAccessToken : ", e);
return null;
}
}
konrad schlatte
10/28/2021, 2:49 PMString responseJsonString = getAccessToken(shopName, code);
if (!StringUtils.isEmpty(responseJsonString)) {
JSONObject responseJson = new JSONObject(responseJsonString);
if (responseJson.has("access_token")) {
String accessToken = responseJson.get("access_token").toString();
user
10/28/2021, 2:50 PMuser
10/28/2021, 2:50 PM