Moz is one of the popular platform offering various tools and services for Search Engine Optimization. Keyword Research, Link Building, SEO Audit and Rank Tracking are some of the key services offered by Moz.
Services provided by Moz are extensively used by freelancers and Digital marketing companies to analyze and track the performance of websites in Seach Engines. For each of the sites, Moz provides analytical and ranking data. Mozrank and Domain authority are two of the key Moz metrics used by Digital marketers to derive the value and ranking potential of a given a website.
- How to iterate Java HashMap
- JaCoCo Code Coverage in Springboot Applications : A Comprehensive Guide
- How to Password Protect a PDF File using Java : A Step-by-Step Guide
To enable Digital Marketers to build SEO solutions, Moz provides API’s (Mozscape API) which can be used to get link and rank metrics for any given website. In this article, we will go through the steps required to call one such API, the URL metrics API. By using this API, we can get metrics like Domain Authority, Page Authority, Moz Rank, Number of external links and many other metrics for a given website. The REST endpoint for the URL Metric API along with the values for the parameters would be in the below format,
https://lsapi.seomoz.com/linkscape/url-metrics/moz.com?Cols=4&Limit=10&AccessID=mozscape-ac52dbcf56&Expires=1545264000&Signature=bc0cc0d590fce266ba8f9fc26b996439
Now we will go through the steps (along wit Java code snippets) that we need to follow to invoke this API to get the desired Domain metric.
1. Get API Credentials
An AccessID and API key is a prerequisite for using any of the Mozscape API. AccessId is more like an username and it uniquely identifies your API account. API key would be your secret password linked to the AccessId. You can change the API key as and when you need.
To get the AccessId and API Key, first you need to signup for Mozscape account (either community account or pro). Once you log in, you can click on ‘Manage Mozscape API Key’ to generate the AccessID and the secret key.
Also Read: How to Generate MD5 Hash in Java
The AccessID we get from this step will be used as the value for AccessID field in the above GET API. The secret key will be used for generating the signature.
2. Prepare Signature
One of the parameters in the URL is the signature. The signature can be obtained by using the HMAC-SHA1 hash of AccessID, Expires (timestamp in seconds to indicate validity of the request) timestamp and secret key. The combination of AccessID and Expires parameter acts as the data and API key acts as a secret key. The generated secure hash should be base 64 encoded and then URL encoded before using as a value for the signature parameter in the request.
public String getHMACSHA1Hash(long timeStamp, String key) throws SignatureException, NoSuchAlgorithmException, InvalidKeyException { String data = AccessID + timeStamp; SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM); Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); mac.init(signingKey); return new String(Base64.getEncoder().encode(mac.doFinal(data.getBytes()))); } return new String(Base64.getEncoder().encode(mac.doFinal(data.getBytes()))); }
3. HTTP call with basic authentication
Two other parameters that we have to pass in the request are the ‘Cols’ and ‘Limit’. The value for ‘Cols’ indicate the Moz metric that we want to fetch. The complete list of Cols values along with its description is available in the Moz API document. The ‘Limit’ parameter is used for limiting the number of results in the response.
The Moz API makes use of Basic authentication for security. As you can see in the below code snippet, I am making use of AccessID and SecretKey for authenticating the request.
parameters.put("Cols", MozUtils.DomainAuthority_Cols); parameters.put("Limit", "10"); parameters.put("AccessID", AccessID); parameters.put("Expires", String.valueOf(timeStamp)); parameters.put("Signature", signature); String urlParams = MozUtils.getParams(parameters); finalURL = finalURL + "?" + urlParams; UriComponents uri = UriComponentsBuilder.fromUri(new URI(finalURL)).build(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor(AccessID, SecretKey)); ResponseEntity<URLResult> response = restTemplate.getForEntity(uri.toUriString(), URLResult.class);
Here I am using Spring Rest Template for making the above HTTP API call.
4. Parse the output and get the Metric
The fields in the response depends on the values you have passed for the ‘Cols’ parameter in the request. You can get the complete list of response fields along with the description from the Moz API document.
URLResult urlResult = response.getBody(); URLMetric urlMetric = new URLMetric(); urlMetric.setDomainAuthority(urlResult.getPda());
By making use of the above steps, I have a developed a wrapper REST API which returns a particular Moz metric given the domain name. If you want a different Moz metric, you just need to change the value against ‘Cols’ parameter in the source code before making the request. You can find the complete source code for this project in GitHub.
Also Read: JaCoCo Code Coverage in Springboot Applications : A Comprehensive Guide
Leave a Reply