Checking if a repo is starred with GitHub API

2019-03-29 19:48发布

问题:

I'm currently attempting to check and see if a repo is starred by an authenticated user using the GitHub API (v3). I can successfully list the repos starred by the logged in user, but I can't seem to get it to work when checking if a repo is starred by the user.

For instance..

(GET) /user/starred?access_token=... correctly returns the full list of repos I have starred.

(GET) /user/starred/joynet/node?access_token=... always returns status 404, no matter what owner/repo combination I use or if I actually have that repo starred or not.

I should also note that I can't seem to access anything at /user/starred except (GET) /user/starred. Any request to PUT, DELETE, or POST to /user/starred/:owner/:repo returns a 404, no matter what I do.

Any idea how I'm supposed to be using this API? I've read the documentation, and this I believe I'm doing it correctly, but I just can't seem to get it work.

回答1:

I was able to solve this problem. The issue was I skimmed the documentation and didn't see I had to request the repo scope during authorization to be able to modify stars :) Simply adding the "repo" scope fixed my issue!

GitHub briefly mentions their scopes here: http://developer.github.com/v3/oauth/ but no where was it mentioned that starring needs the "repo" scope.



回答2:

You are using the API right. According to the documentation if you have not starred a repository (or the repository or the user, or both, does not exists) you will get a 404 status, otherwise you will get a 204. You're getting always a 404 status because you have misspelled the username joyent.

So, to check if the authenticated user starred the node repository:

GET /user/starred/joyent/node

To star the node repo:

PUT /user/starred/joyent/node

And to unstar the node repo:

DELETE /user/starred/joyent/node

Also, if you try to make a PUT or DELETE request to /user/starred/:owner/:repo with an owner or repository that doesn't exists you will get a 404 status. So, you were probably getting all these 404 statuses because of the misspelled username.