When creating a new repo using the GitHub API with Apps Script, I'm getting response code 404 with the error:
{"message":"Not Found","documentation_url":"https://developer.github.com/v3"}
The token ID that I'm using works for creating a new file, so I know that the token ID is valid. Also, the user ID that I'm using works for creating a new file with some other Apps Script code.
The documentation gives a url ending of:
POST /user/repos
I'm assuming that "user" needs to be replaced with the user name, and "repos" needs to be replaced with what the new repo name will be.
The url that I'm trying is:
https://api.github.com/blueprinter/AAA_NewRepo
I've also tried the url:
https://api.github.com/blueprinter/repos/AAA_NewRepo
I've also tried adding a name key to the options object:
name:po.repoName
I've also tried adding a payload with an object with a name key to the stringify payload object:
data = {
name:po.repoName
}
payload = JSON.stringify(data);
options = {
method: 'post',
muteHttpExceptions: true,
contentType: "application/json",
headers: {
Authorization: "Bearer " + myToken
},
responseType: 'json',
payload:payload
}
The code:
function createNewRepo_(po) {
try{
var data,myToken,options,payload,rslt,rspnsCode,apiBaseUrl,url;
/*
https://developer.github.com/v3/repos/#create
POST /user/repos -
po.userName - The user name in the GitHub account
po.repoName - The name of the repository to put the file into
*/
//Logger.log('po: ' + JSON.stringify(po))
apiBaseUrl = 'https://api.github.com';//Every url must have this at the beginning
if (!po.userName) {
po.userName = getMyGitHubInfo_('userName');
}
url = apiBaseUrl + "/" + po.userName + "/" + po.repoName;
Logger.log('url 23: ' + url)
myToken = getGitHubToken();
payload = JSON.stringify(data);
options = {
method: 'post',
muteHttpExceptions: true,
contentType: "application/json",
headers: {
Authorization: "Bearer " + myToken
},
responseType: 'json'
}
//Logger.log('options 39: ' + JSON.stringify(options))
rslt = UrlFetchApp.fetch(url,options);
rspnsCode = rslt.getResponseCode();
Logger.log('rspnsCode 44: ' + rspnsCode)
if (rspnsCode !== 200 && rspnsCode !== 201) {
throw new Error('Response coming back from GitHub is: ' + rspnsCode + "\n\n" + rslt.getContentText());
}
Logger.log('rslt.getContentText(): ' + rslt.getContentText())
Logger.log('typeof rslt: ' + typeof rslt)
data = JSON.parse(rslt);//Even though the returned value is an object it must be parsed into JSON
Logger.log('data' + JSON.stringify(data))
}catch(e) {
Logger.log("Error: " + e.message + "\nStack: " + e.stack)
//errorHandling(e);//Run centralized error handling function
return false;
}
}
function getMyGitHubInfo_(k) {
var o;
o = {
userName:'git_hub_user_ID_Here',//Your user name
}
if (k) {
return o[k];
} else {
return o;
}
}
/user/repos?(no replacement). Also, show how you get the access token. - TheMasteroptionsis used, please request usingUrlFetchApp.fetch("https://api.github.com/user/repos", options). Ref In this case, please addrepoto the scopes. Please be careful this. - Tanaike/user/reposwithout replacing anything in the url, but then created an object with anamekey, and a value of the new repo name, then stringified the object and added it as the payload, and it worked. So, I was using the wrong url. I shouldn't have been trying to replaceuserwith the user ID, orreposwith the repo name. That url isn't meant to be modified, which I didn't understand. About the only thing left was adding the repo name in the payload, so I finally figured that out. - Alan Wells