Test platform series (38) Access github Third-party Login (2)

Hello everyone, this is Milo, a blogger who wants to share with you the techniques, interviews and growth experiences related to test development!

Welcome everyone to pay attention to my public number: test development pit goods.


In the last part, we thought of a specific idea, and this part will realize it.

implementation

In fact, the back-end part is very simple. You only need to complete the next two steps, that is, generate the token and obtain the user information.

  • Write the register_for_github method

    When we get the user information from Github, we write it to our own user table, update the user information if there is one, and add a new record if there is none.

If there is a user whose email and username match, we will update the nickname and profile picture fields of the user and update the last login time.

If there is no user, we create a new user. A few random digits are salted to ensure that the user’s password is random. If a user logs in through Github, it is almost impossible to try out the user’s password on the test platform.

  • Write the Github login interface

The request parameter is code, and the Session class for requests is used here because you need to send two HTTP requests.

  1. Get access_token

Url: github.com/login/oauth…

Method: Either GET or POST is acceptable

Parameters: code, clientid, and Secret, which are the two fields required in the previous section

The returned data looks like this:

A simple string of text from which we need to get the access_token.

  1. Obtaining user Information

url: api.github.com/user

method: GET

Headers: {“Authrozation”: “token “}

So you can get the user information:

You can see the login, email, name, other useful information we can use the avatar_URL, so that we can directly take each other’s profile picture to show.

The front part

I have already implemented the front-end part, the general idea is to provide github login button, when the user enters the page to determine whether the URL contains? Code =, if yes, it indicates that it is a callback event after github login succeeds, and then the code is parsed and the login interface written above is invoked. Other logic is consistent with that of ordinary user login.

If you are interested, you can study the specific implementation, write rough, you can see the corresponding changes through the submission record ~

Github speed can refer to this, hands-on testing effective:

zhuanlan.zhihu.com/p/107334179

Online demo address: http://47.112.32.195/

Front-end code repository: github.com/wuranxu/pit…

Back-end code repository: github.com/wuranxu/pit…