Created
July 9, 2018 13:47
-
-
Save atechcrew/126858c2937599fe97fc535e6fe2e9e9 to your computer and use it in GitHub Desktop.
Java code to check domain and page authority using MOZ API. Check domain and page authority with MOZ API using java code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.net.URLEncoder; | |
import java.util.Base64; | |
import java.util.Scanner; | |
import javax.crypto.Mac; | |
import javax.crypto.spec.SecretKeySpec; | |
public class DaPaChecker { | |
public static void main(String args[]) throws Exception{ | |
String moz_ID = ""; | |
String moz_key = ""; | |
String website = ""; | |
String requestUrl = getEncodedURL(moz_ID, moz_key, website); | |
Scanner scan = new Scanner(new URL(requestUrl).openStream()); | |
String result = scan.nextLine(); | |
scan.close(); | |
System.out.println(result); | |
String domainAuthority = result.split(",")[2].split(":")[1].replaceAll("}", ""); | |
String pageAuthority = result.split(",")[1].split(":")[1]; | |
System.out.println("Domain Authority = " + domainAuthority); | |
System.out.println("Page Authority = " + pageAuthority); | |
} | |
public static String getEncodedURL(String accessID, String secretKey, String objectURL) throws Exception{ | |
long expires = (System.currentTimeMillis() / 1000L) + 300; | |
String stringToSign = accessID + "\n" + expires; | |
String binarySignature = generateSignature(stringToSign, secretKey); | |
String urlSafeSignature = URLEncoder.encode(binarySignature, "UTF-8"); | |
String cols = "103079215108"; | |
return "http://lsapi.seomoz.com/linkscape/url-metrics/" + URLEncoder.encode(objectURL, "UTF-8") + "?Cols=" + cols + | |
"&AccessID=" + accessID + "&Expires=" + expires + "&Signature=" + urlSafeSignature; | |
} | |
public static String generateSignature (String stringToSign, String key) throws Exception { | |
String encoded = ""; | |
String type = "HmacSHA1"; | |
Mac HMAC = Mac.getInstance(type); | |
SecretKeySpec secretKey = new SecretKeySpec((key).getBytes("UTF-8"), type); | |
HMAC.init(secretKey); | |
byte[] Hash = HMAC.doFinal((stringToSign).getBytes("UTF-8")); | |
encoded = Base64.getEncoder().encodeToString(Hash); | |
return encoded; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment