The content of this article source: Https://www.dataquest.io/mission/117/working-with-apis
Data source for this article: https://en.wikipedia.org/wiki/International_Space_Station
Abstract: Use the requests library and GitHub APIs to familiarize yourself with the process of interacting with network data by manipulating the GitHub repository
The role of the API (application program Interface) is to query and return dynamic Data on the web, such as stock price information and real-time commentary on the news. The process of getting data through an API is like accessing a Web page, submitting a request to the server, and then returning the appropriate data to the server. Typically using the requests library (http://www.python-requests.org/en/latest/) to submit the request, the GitHub API address used in this article is: https://developer.github.com/v3/
Get access token
To manipulate your GitHub through the API, you must first generate an access token on the GitHub URL (https://github.com/settings/tokens)
There are two reasons to use an access token instead of a user name and password:
- Usually put in the program in the user name and password is very easy to leak, others see your code and know the user name and password can be arbitrary operation. And if someone uses your access token, you can log out of that token at any time.
- An access token is a fine-grained permission configuration that you can set to view only your GitHub, thus ensuring security
Once you get the access token, remember to copy it right away and record it in the program, and all requests must carry the access token, remembering that the token field must be
{"Authorization": "Token 975ef3a9dff1a7dc50d5f0a4341ecf5a89f201ab"}
Note: All occurrences of kylinlin in this article are replaced with your account name
GET request
Get requests the user to get information from the server
#get information about an authorized userImportrequestsheaders= {"Authorization":"975ef3a9dff1a7dc50d5f0a4341ecf5a89f201ab"}#The first two rows are ignored in the following code, not writtenUser= Requests.get ('Https://api.github.com/user', headers=headers). JSON ()PrintUser#If there is no headers parameter in the Get () method, the following result is returned: (indicates authentication required){u'Documentation_url': U'Https://developer.github.com/v3', u'message': U'Requires Authentication'but not all methods have to take the headers parameter to get personal information (replace Kylinlin with any account name can) response= Requests.get ("Https://api.github.com/users/kylinlin")PrintResponse.status_codePrintResponse.json ()
Each access generates a status code, represented by the attribute Status_code, which is used to represent the result of the visit:
- 200--Everything is normal and returns the result of the request
- 301--The server redirects the request to another Web site
- 401--Authentication error
- 400--The server thinks this is a bad request
- 403-You do not have access to the resource
- 404--access to a resource does not exist
The content returned by a request is a string format by default, and the most convenient way to extract some information from that content is to convert the string to a Python object, and the JSON () method is used to implement this function.
In the Get method, the params parameter in the Requests.get () method is the parameter in the URL, and unlike the headers parameter, the value of the headers parameter is written in the request header, and the value of the params parameter is written in the URL. For example, to get the project that the GitHub author is interested in, and 50 results as one page, get the content of the first page. At this point the URL is actually like this: https://api.github.com/users/kylinlin/starred?pre_page=50&page=1
params = {"per_page""page": 1= Requests.get (" https://api.github.com/users/kylinlin/starred", params== Response.json () Print Page1_repos
POST request
Post requests are used to send information to the server and then create objects on the server, such as creating a new project
Payload = {'name':'learning-about-apis'} # The value of the name parameter is the name of the project to create = requests.post ('https://api.github.com/user/repos', The Headers# JSON parameter translates the payload variable into a dictionary format print status # Output 201 indicates that the project was created successfully
Patch and put requests
Sometimes we just want to update the information for a project, and then we can use the patch or Put method, for example, to update the description of the project you just created
Payload = {'Description':'Learning about requests!','name':'Learning-about-apis'}status= Requests.patch ('Https://api.github.com/repos/kylinlin/learning-about-apis', Json=payload, headers=headers). Status_codePrintStatus#when the patch method succeeds, it returns
Delete request
Delete requests are used to remove objects on the server, such as to delete a project that was just
Status = Requests.delete ('https://api.github.com/repos/kylinlin/learning-about-apis' , headers=print status# Output 204 indicates successful deletion
Several requests for the requests library-using the API to manipulate GitHub