L o a d i n g . . .
主打一个C++
文章详情

网站接入github实现第三方用户登录

Posted on 2022-02-15 13:49:06 by 主打一个C++

官方文档: https://docs.github.com/en/apps

//请求登录地址https://github.com/login/oauth/authorize?client_id=用户公匙

(登录过期的情况下会直接弹出登录窗口)

image.png

//请求登录参数简介

询问参数类型必需的?描述
client_idstring需要当您执行以下操作时,您从GitHub收到的客户端ID注册的.
redirect_uristring强烈推荐授权后用户将被发送到的应用程序中的URL。请参见下面的详细信息重定向URL
loginstring可选择的建议用于登录和授权应用程序的特定帐户。
scopestring上下文相关以空格分隔的列表领域。如果没有提供,scope对于没有为应用程序授权任何范围的用户,默认为空列表。对于拥有应用程序授权范围的用户,不会向用户显示OAuth授权页面和范围列表。相反,流程的这一步将自动完成用户为应用程序授权的范围集。例如,如果用户已经执行了web流两次,并且已经使用user范围和另一个带有repo范围,第三个web流不提供scope将收到一个令牌user和repo范围。
statestring强烈推荐不可访问的随机字符串。它用于防止跨站点请求伪造攻击。

//返回类型: GET 参数:code (上方请求格式的返回,非固定) (用于获取用户token),通过 $_GET['code'] 直接获取。

//下面直接拼接post请求地址 https://github.com/login/oauth/access_token  获取用户 token

$popst = 'https://github.com/login/oauth/access_token?client_id=用户公匙&client_secret=用户密匙&code=' . $_GET['code'] .'&redirect_uri=http/s:// 你的回调路径'

//获取用户token参数解释,可参见: 参数解释

参数名称类型必需的?描述
请求链接/格式POST必须(格式:GET)https://github.com/login/oauth/access_token?client_id=参数1&client_secret=参数2&code=参数3
client_idstring需要您从GitHub收到的OAuth应用程序的客户端ID。
client_secretstring需要您从GitHub收到的OAuth应用程序的客户端密码。
codestring需要您收到的作为步骤1响应的代码。
redirect_uristring强烈推荐授权后用户被发送到的应用程序中的URL。我们可以用它来匹配最初提供的URIcode以防止对您的服务的攻击。

//得到token数据 access_token

access_token=gho_U2qGzOE3i0wgvJIss123dWI85Ka4Ctd92qGzIhuWOE3i0mwy&scope=&token_type=bearer

//最后就可以直接获取用户信息了

//请求类型:GET / POST  地址: https://api.github.com/user

//请求头:array('Authorization: ' . $_GET['token_type'] .' '. $_GET['access_token'])'

'', 请求头:array('Authorization: ' . $output['token_type'] .' '. $output['access_token'])'

//最后获取到这个JSON对象包含了GitHub用户的一些基本信息

这里json格式是我图方便直接同步请求得来的,如下:

{
  "login": "test",
  "id": 104769752,
  "node_id": "U_kgD9Bj682A",
  "avatar_url": "https://avatars.githubusercontent.com/u/194769759?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/test",
  "html_url": "https://github.com/test",
  "followers_url": "https://api.github.com/users/test/followers",
  "following_url": "https://api.github.com/users/test/following{/other_user}",
  "gists_url": "https://api.github.com/users/test/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/test/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/test/subscriptions",
  "organizations_url": "https://api.github.com/users/test/orgs",
  "repos_url": "https://api.github.com/users/test/repos",
  "events_url": "https://api.github.com/users/test/events{/privacy}",
  "received_events_url": "https://api.github.com/users/test/received_events",
  "type": "User",
  "site_admin": false,
  "name": null,
  "company": null,
  "blog": "",
  "location": null,
  "email": null,
  "hireable": null,
  "bio": null,
  "twitter_username": null,
  "public_repos": 0,
  "public_gists": 0,
  "followers": 0,
  "following": 0,
  "created_at": "2022-05-02T12:18:17Z",
  "updated_at": "2024-09-01T06:35:27Z"
}

//顺便对key进行解释如下:

- `login`: 用户的登录名或用户名。
- `id`: 用户在GitHub上的唯一标识符。
- `node_id`: 用户在GitHub API中的节点ID。
- `avatar_url`: 用户头像的URL地址。
- `gravatar_id`: Gravatar(全球公认头像)的ID,这里为空表示用户没有设置Gravatar头像。
- `url`: 用户在GitHub API中的URL地址。
- `html_url`: 用户在GitHub网站上的个人主页URL。
- `followers_url`: 获取用户关注者的API URL。
- `following_url`: 获取用户正在关注的其他用户的API URL,`{/other_user}`表示可以指定具体的用户。
- `gists_url`: 获取用户gist(代码片段)的API URL,`{/gist_id}`表示可以指定具体的gist ID。
- `starred_url`: 获取用户 starred(收藏)的仓库的API URL,`{/owner}{/repo}`表示可以指定具体的仓库所有者和仓库名。
- `subscriptions_url`: 获取用户订阅的仓库的API URL。
- `organizations_url`: 获取用户所属组织的API URL。
- `repos_url`: 获取用户仓库的API URL。
- `events_url`: 获取用户事件的API URL,`{/privacy}`表示可以指定隐私级别。
- `received_events_url`: 获取用户接收事件的API URL。
- `type`: 用户类型,这里是`User`表示普通用户。
- `site_admin`: 是否为GitHub管理员,`false`表示不是。
- `name`: 用户的真实姓名或显示名称,这里为`null`表示未设置。
- `company`: 用户所属公司,这里为`null`表示未设置。
- `blog`: 用户的博客地址,这里为空字符串表示未设置。
- `location`: 用户的地理位置,这里为`null`表示未设置。
- `email`: 用户的电子邮件地址,这里为`null`表示未设置。
- `hireable`: 用户是否可被雇佣,这里为`null`表示未设置。
- `bio`: 用户的个人简介,这里为`null`表示未设置。
- `twitter_username`: 用户的Twitter用户名,这里为`null`表示未设置。
- `public_repos`: 用户公开的仓库数量,这里是`0`表示没有公开仓库。
- `public_gists`: 用户公开的gist数量,这里是`0`表示没有公开gist。
- `followers`: 用户的关注者数量,这里是`0`表示没有关注者。
- `following`: 用户正在关注的其他用户数量,这里是`0`表示没有关注其他用户。
- `created_at`: 用户账户创建的时间。
- `updated_at`: 用户信息最后更新的时间。


*转载请注明出处:原文链接:https://cpp.vin/page/17.html

作者近期文章
提示
×
确定
数据库执行: 7次 总耗时: 0.01s
页面加载耗时: 



wechat +447752296473
wechat cpp-blog