OAuth2.0在Github上的认证操作

1.网站与Github协商

  • 在github上注册应用

  • 设置所需的用户权限

  • Github发放Client_IdClient_Secret

2. 用户与GitHub协商

  • 用户 + Client_Id进入授权页面
// 用户登录 github,协商
	GET //github.com/login/oauth/authorize
	// 协商凭证
	params = {
	client_id: "xxxx",
	redirect_uri: "http://my-website.com"
	}
  • 用户

    • 拒绝——Oauth认证结束

    • 同意——跳转之前设定的redirect_url?code=xxx

      // 协商成功后带着盖了章的 code
      	Location: http://my-website.com?code=xxx
      

3. 网站拜访Github

  • Code + client_Id + client_secret拜访Github
// 网站和 github 之间的协商
    POST //github.com/login/oauth/access_token
// 协商凭证包括 github 给用户盖的章和 github 发给我的门票
    params = {
      code: "xxx",
      client_id: "xxx",
      client_secret: "xxx",
      redirect_uri: "http://my-website.com"
    }	
  • 获得access_token
  // 拿到最后的绿卡
  response = {
    access_token: "e72e16c7e42f292c6912e7710c838347ae178b4a"
    scope: "user,gist"
    token_type: "bearer",
    refresh_token: "xxxx"
}

4.网站访问用户数据

通过api?access_token = xxx访问用户数据

// 访问用户数据
  	GET //api.github.com/user?access_token=e72e16c7e42f292c6912e7710c838347ae178b4a

  // 告诉我用户的名字和邮箱
  response = {
    username: "barretlee",
    email: "barret.china@gmail.com"
  }
posted @ 2017-11-09 12:44  iaei  阅读(195)  评论(0)    收藏  举报