【问题标题】:Get a token by Github API通过 Github API 获取令牌
【发布时间】:2017-06-12 06:26:06
【问题描述】:

我在Github -> Settings -> Personal access tokens -> Generate new token 中手动创建了一个令牌,并且只选择了repo scope

此令牌工作正常,因此我可以使用它进入我拥有write 权限的组织。

然后我想通过github-api 做同样的事情(获取 access_token)。

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'
}

一切都是正确的,但我无法进入我拥有write 权限的组织回购。我只能推送到我自己的仓库中。

你能帮忙吗?欢迎提出任何错误或不准确之处。

【问题讨论】:

  • 你的问题很模糊。我在答案中发布了两种可能的解决方案。如果它们不正确,您将真的需要更新您的问题,因为细节太少。
  • Roma,您能看看我的回答,如果您还有其他问题,请告诉我?

标签: git github access-token github-api


【解决方案1】:

因此,如果您想通过 GitHub 的 API 执行此操作,您的请求需要更改。

首先你需要像这样使用/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 登录的应用程序。如果是这种情况,那么你需要专门关注Web Application Flow for OAuth

在这种情况下,您是其中的一部分,但您发送了错误的参数。

首先你发出一个 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 请求吗?如何制作一个系统,在第一次注册时只询问电子邮件权限,并说当用户尝试观看回购时,然后向用户询问回购范围授权。我错过了什么?
【解决方案2】:

通过在基本授权验证中传递您的client_idclient_secret,使用带有URL https://api.github.com/authorizations 的POST。 将正文中的其余 json 格式的参数作为 raw 发送。
例如:

{
  "scopes": 
    [
      "repo",
      "write:org"
    ],
  "note": "Sample Access Token using API Call",
  "fingerprint": "DEMO#$12@A"
}

【讨论】:

    猜你喜欢
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 2015-03-05
    • 2015-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    相关资源
    最近更新 更多