Just as a buffer, I’m not super familiar with anything other than iOS development and don’t have a fat lot of experience with oAuth but I was trying to make a little personal app which includes signing in with GitHub (using oAuth) and then making a request to fetch my private repos.
I’ve hit a wall, I’m absolutely stumped and I'm looking for some pointers. I’ve been reading the documentation on GitHub for most of the day and still can’t grasp what the actual issue is. I’ve tried copious different url request variants and I whilst I can return my public repos, I keep hitting a while wall whilst trying to get a list of ALL my repos, including private repos.
I’ve been followed the api docs for getting the list of repos for an authenticated users. I can see I have complete read write access for all private repos for my register ‘GitHub oAuth App’.
I have my access token which has no expiry on so I know that’s okay. I keep either just hitting a 400, 401 or 403 error when trying various methods.
The code below works just fine for public repos but not for fetching my private repos. What am I missing? Absolutely tearing my hair out at this point.
let publicRepoRequest = NSMutableURLRequest(url: NSURL(string: "h ttps://api.github.com/users/\(username)/repos")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
let headers = [
"Accept": "application/ vnd.github+json",
"Authorization": "Bearer \(accessToken)",
"X-GitHub-Api-Version": "2022-11-28"
]
let privateRepoRequest = NSMutableURLRequest(url: NSURL(string: "h ttps://api.github.com/user/repos")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
privateRepoRequest.httpMethod = "GET"
privateRepoRequest.allHTTPHeaderFields = headers
let task = URLSession.shared.dataTask(with: privateRequest as URLRequest) { data, response, error in
if let error {
print("E01: \(error.localizedDescription).")
} else {
let httpResponse = response as? HTTPURLResponse
if httpResponse?.statusCode == 200 {
if let data {
do {
let decoder = newJSONDecoder()
let repos = try decoder.decode([Repo].self, from: data)
for repo in repos {
print("repo name: \(repo.name)")
}
} catch {
print("E04 - unable to decode data.")
}
} else {
print("E03 - no data.")
}
} else {
print("E02 - status code: \(httpResponse?.statusCode).")
}
}
}
task.resume()
(Edited to try and remove formatting on strings, the space between the forward slash on "application" etc is removed in code. The formatting was just weird on here.)