It throws a 403 Forbidden
error because it uses Github GET contents API which gives results for file not exceding 1Mo. For instance the following will throw 403 :
https://api.github.com/repos/bertrandmartel/w230st-osx/contents/CLOVER/tools/Shell64.efi?ref=master
Using this method using GET tree API, you can get the file sha without downloading the whole file and then use repo.getBlob
(which uses Get blob API for file not exceding 100Mo).
The following example will get the tree for the parent folder of the specified file (for a file exceding 1Mo) with the GET trees api, filter the specific file by name and then request blob data :
const accessToken = 'YOUR_ACCESS_TOKEN';
const gh = new GitHub({
token: accessToken
});
const username = 'bertrandmartel';
const repoName = 'w230st-osx';
const branchName = 'master';
const filePath = 'CLOVER/tools/Shell64.efi'
var fileName = filePath.split(/(\\|\/)/g).pop();
var fileParent = filePath.substr(0, filePath.lastIndexOf("/"));
var repo = gh.getRepo(username, repoName);
fetch('https://api.github.com/repos/' +
username + '/' +
repoName + '/git/trees/' +
encodeURI(branchName + ':' + fileParent), {
headers: {
"Authorization": "token " + accessToken
}
}).then(function(response) {
return response.json();
}).then(function(content) {
var file = content.tree.filter(entry => entry.path === fileName);
if (file.length > 0) {
console.log("get blob for sha " + file[0].sha);
//now get the blob
repo.getBlob(file[0].sha).then(function(response) {
console.log("response size : " + response.data.length);
});
} else {
console.log("file " + fileName + " not found");
}
});