google-apps-scriptgoogle-sheetsweb-scrapingopensea

Get OpenSea profile data using Google Apps Script into Google Sheets


I am trying to get Twitter link from OpenSea profile using the following Google Apps Script sample code:

function getTwitterLink() {

  var url = "https://opensea.io/0xe7dAe42Dee2BB6C1ef3c65e68d3E605faBcA875d";
  var response = UrlFetchApp.fetch(url).getContentText();
      
  var regex = /https:\/\/twitter.com\/[^"']+/;
  var match = regex.exec(response);
  
  if (match && match.length > 0) {
    var twitterLink = match[0];
    Logger.log("Twitter Link: " + twitterLink);
    return twitterLink;
  } else {
    Logger.log("Twitter Link not found.");
    return null;
  }
}

Here is the screenshot of the profile, you can find the link embedded in the Twitter logo on right most side:

Twitter Link

However, it gives the following error when it runs:

Exception: Request failed for https://opensea.io returned code 403. Truncated server response: Just a moment... <meta http-equiv="Content-Type" content="text/html; charset=UTF-... (use muteHttpExceptions option to examine full response)

I think this is because I need to use API key to get this Twitter link. So I reviewed OpenSea API Documentation. But unable to find any relevant endpoint. I would really appreciate it if you can guide me to get this. Thank you


Solution

  • When I looked at the documentation, I found the endpoint to get Twitter account information, so here is the proposed script:

    function getTwitterLink() {
    
     var url = "https://api.opensea.io/api/v1/user/0xe7dAe42Dee2BB6C1ef3c65e68d3E605faBcA875d";
    
     var options = {
        method: 'GET',
        headers: {
          'X-API-KEY': 'YOUR_API_KEY'
        },
        muteHttpExceptions: true
      };
      
     var response = UrlFetchApp.fetch(url, options);
     var payload = JSON.parse(response.getContentText());
     var twitterLink = "https://twitter.com/" + payload.account.twitter_username;
     console.log(twitterLink)
     }
    

    Let me know if it helps.