簡體   English   中英

通過Github API獲取令牌

[英]Get a token by Github API

我在Github -> Settings -> Personal access tokens -> Generate new token手動創建了一個令牌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權限的組織回購。 我只能推進自己的回購。

你能幫忙嗎? 任何想法錯誤或不准確的地方都是受歡迎的。

因此,如果您想通過GitHub的API執行此操作,您的請求需要更改。

首先,您需要使用/authorizations端點,如下所示:

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登錄的應用程序。 如果是這種情況,那么您需要專門遵循OAuthWeb應用程序流程

在這種情況下,你是那里的一部分,但你發送錯誤的參數。

首先你提出一個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>

干杯!

通過在基本身份驗證授權中傳遞client_idclient_secret ,將POST與url https://api.github.com/authorizations一起使用。 以body中的json格式發送剩余參數作為raw。
例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM