通过 Github API 获取令牌

15

我在 Github 的 “设置 -> 个人访问令牌 -> 生成新令牌” 中手动创建了一个令牌,并仅选择了“repo”和“scope”。

这个令牌可以正常工作,因此我可以将它用于我拥有“写入”权限的组织中进行推送。

然后我想通过 github-api 做相同的事情(获取访问令牌)。

params = dict(client_id=client_id,
              client_secret=client_secret,
              code=code)

url = url_concat("https://github.com/login/oauth/access_token", params)

req = HTTPRequest(url,
                  method="POST",
                  headers={"Accept": "application/json"},
                  body="") 

因此我有这样的json

{
    'scope': 'repo',
    'token_type': 'bearer',
    'access_token': 'xxxxxxxx10755fbb6c281e92902ed122144886c5'
}

一切正常, 但我无法将代码推送到我拥有写权限的组织库中,只能推送到我自己的库中。

能帮忙吗? 欢迎任何想法或建议。


你的问题非常模糊。我在我的答案中发布了两个可能的解决方案。如果它们不正确,你就需要真的更新你的问题,因为细节太少了。 - Ian Stapleton Cordasco
Roma,你能否看一下我的答案并告诉我是否还有其他问题? - Ian Stapleton Cordasco
2个回答

17
所以,如果你想通过GitHub的API来实现这一点,你的请求需要改变。
首先,你需要使用`/authorizations`端点,像这样:/authorizations endpoint
POST /authorizations
Authorization: Basic ...
Content-Type: application/json
Content-Length: ...

{
  "scopes": [
    "repo",
    "write:org"
  ],
  "note": "Example from StackOverflow by @sigmavirus24",
  "client_id": "Your client_id here",
  "client_secret": "Your client_secret here",
  "fingerprint": "1234",
}

然后应该返回一个带有以下内容的正文的201 Created响应:

{
  "id": 72249124,
  "url": "https://api.github.com/authorizations/72249124",
  "scopes": [
    "repo",
    "write:org"
  ],
  "token": "abcdefgh12345678",
  "token_last_eight": "12345678",
  "hashed_token": "25f94a2a5c7fbaf499c665bc73d67c1c87e496da8985131633ee0a95819db2e8",
  "app": {
    "url": "http://my-github-app.com",
    "name": "my github app",
    "client_id": "abcde12345fghij67890"
  },
  "note": "optional note",
  "note_url": "http://optional/note/url",
  "updated_at": "2017-02-08T20:39:23Z",
  "created_at": "2017-02-08T17:26:27Z",
  "fingerprint": "1234"
}

除了它将是真实的。

话虽如此,看起来您正在尝试使用允许GitHub用作身份验证提供程序的端点。换句话说,您正在构建一个应用程序,允许用户使用GitHub登录。如果是这种情况,则需要特别遵循OAuth Web应用程序流程

在这种情况下,您已经完成了一部分,但发送错误的参数。

首先,您发出一个GET请求:

GET https://github.com/login/oauth/authorize?client_id=<your-client_id>&scopes=repo%20write:org&state=something-random

然后你将从GitHub收到数据,你必须在POST请求中使用它。

POST https://github.com/login/oauth/access_token?client_id=<your-client_id>&client_secret=<your-client_secret>&code=<code-from-github>
Accept: application/json

之后,您发出的任何请求都必须具备

Authorization: token <token-received-in-response-to-POST>

加油!


嗨,我有点困惑。我们不需要为使用特定范围进行新令牌的POST请求而获取所有范围吗? 如何创建一个系统,在第一次注册时只询问电子邮件权限,当用户尝试观看存储库时,再向用户请求存储库范围授权。我错过了什么? - Dhiraj Barnwal
/authorizations 端点已于2020年11月被移除。https://developer.github.com/changes/2/#deprecation-timeline - brian d foy

1
使用POST方法与链接https://api.github.com/authorizations配合使用,在“Authorization”的“Basic Auth”中传递您的client_idclient_secret。 将其余参数以JSON格式作为原始数据发送到请求正文中。
例如:
{
  "scopes": 
    [
      "repo",
      "write:org"
    ],
  "note": "Sample Access Token using API Call",
  "fingerprint": "DEMO#$12@A"
}

"/authorizations" 端点已于2020年11月被移除 - https://developer.github.com/changes/2/#deprecation-timeline - brian d foy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接