Search API Connector Documentation
Import GitHub Data to Google Sheets
In this guide, we’ll walk through how to pull data from the GitHub API directly into Google Sheets, using the API Connector add-on for Google Sheets. We’ll first handle authentication, and then set up a request to pull in code repository details from GitHub to your spreadsheet.
There are 2 ways to connect to the GitHub API:
- Preset “Connect” button (OAuth) premium
- Personal access token. Please check the appendix for detailed instructions to retrieve your token.
Contents
- Before You Begin
- Part 1: Connect to the GitHub API
- Part 2: Create your API Request URL
- Part 3: Pull Github API data into Sheets
- Part 4: More Example GitHub API URLs
- Part 5: Handle Pagination
- Part 6: API Documentation
- Appendix: Connect with a Personal Access Token
Before You Begin
Click here to install the API Connector add-on from the Google Marketplace.
Part 1: Connect to the GitHub API
We’ll first initiate the connection to the GitHub API.
- Open up Google Sheets and click Extensions > API Connector > Manage Connections.
- In the list of available connections, find GitHub and click Connect.
- You will see a modal asking you to approve the connection. Click Authorize.
- You’ll then be returned to your Google Sheet, and can verify that your GitHub connection is active in the Connections screen.
Part 2: Create Your API Request URL
We’ll first search for repositories matching a specific search term.
- API root: https://api.github.com
- Endpoint: /search/repositories
- Parameters: q=SEARCH_TERM
Putting it all together, we get a full API Request URL:
https://api.github.com/search/repositories?q=google tag manager
Part 3: Pull GitHub API Data into Sheets
We can now enter our URL into API Connector.
- Open up Google Sheets and click Extensions > API Connector > Open.
- In the Create tab, enter the API URL we just created.
- Under OAuth, choose GitHub from the drop-down menu.
- We don’t need any headers, so just skip that section.
- Create a new tab and click Set current to use that tab as your data destination.
- Name your request and click Run. A moment later you’ll see GitHub data populate your Google Sheet:
Part 4: More Example GitHub API URLs
You can experiment with endpoints as described in the documentation to see other types of GitHub data. If you just want to jump in and get an idea, you can play around with the URLs you enter in the API URL path field:
- Get your own (public) repositories
https://api.github.com/user/repos
- Get a repository’s issues. Substitute in the repo’s owner and the name of the repo.
https://api.github.com/repos/{owner}/{repo}/issues?state=all&per_page=100
- Get a repository’s contributors. Substitute in the repo’s owner and the name of the repo.
https://api.github.com/repos/{owner}/{repo}/contributors?per_page=100
Part 5: Handle Pagination
- By default, GitHub limits the number of records returned at once, usually to 30 or 100 at a time.
- As an alternative to changing these parameters manually, you can loop through and grab all your data automatically with pagination handling (paid feature). GitHub’s documentation shows that next page URLs are provided in a field called “Link”, so you would paginate through responses like this:
API URL: enter your request URL, including per_page=100
Pagination type:next page URL
Next page path:Link
Run until: choose when to stop fetching data
Part 6: API Documentation
Official API documentation: https://docs.github.com/en/rest/reference
Appendix: Connect with a Personal Access Token
- Log in to Github and navigate to https://github.com/settings/tokens. Click either “Generate a personal access token” or the “Generate new token” button.
- This will take you to a screen to set up scope details for your token. For our purposes here we don’t need any additional scopes, so just choose an expiration date and enter a description in the Note field.
- Click the “Generate token” button at the bottom of the page:
- Your GitHub personal access token is now ready:
- One more step: Basic Access Authentication requires us to encode our authentication info to base 64. You can do this opening up Developer Tools in your browser (F12 on Windows/Linux or option + ⌘ + J on OSX). In the console, type in the following and click enter:
encodedData = "Basic " + window.btoa('YOUR_USERNAME+YOUR_PERSONAL_ACCESS_TOKEN')
Substitute your own Github username and personal access token values from above. It should look like this (don’t forget the plus sign in the middle):
- Copy the output that appears in Developer Tools to your clipboard, excluding the quotation marks at the beginning and end of the string. (If you have any problems with the above, check the article on Encoding Credentials to Base 64 (Basic Authentication) for alternative methods).
- Now, when you run your request, leave OAuth set to None and enter a key-value pair in the Headers section, where Key = Authorization and Value = Basic YOUR_ENCODED_TOKEN:
It is possible to do a POST request that allow us to create an invitation for a new member or collaborator in the organization?
I’m not really familiar with this part of Github’s API, but I took a quick look at the documentation and maybe this is what you’re looking for? https://developer.github.com/v3/orgs/members/#create-organization-invitation
Thanks you for this, Ana (-: