# 获取access_token

# 介绍

​Server 端 Http API 的鉴权基于 access_token,所有需要 access_token 参数访问的Http API,都要通过如下接口获取 access_token。access_token 有效期一般为 1 小时,失效后需要重新获取。

  • 为通过OAuth2验证,需在请求的URL末尾加上 access_token=获取到的access_token。 access_token 可以通过 获取access_token 接口获取(不参与签名),有效期一般为 1 小时。
  • 在access_token有效期内访问 获取access_token 接口将返回原access_token,不会更新access_token。
  • access_token失效后,使用access_token访问受保护接口会返回401错误(详见 获取access_token 接口的“访问受保护资源”小节),此时应调用获取access_token接口获取新的access_token。

# 获取Access Token API

POST https://game.kwai.com/openapi/oauth/token

POST Body

Content-Type:application/x-www-form-urlencoded

属性 类型 说明
grant_type string 填固定值 client_credentials
client_id string 从快手开放平台获取的 app_id
client_secret string 从快手开放平台获取的 app_secret

举例:

https://game.kwai.com/openapi/oauth/token?grant_type=client_credentials&client_id=APPID&client_secret=APPSECRET

返回结果

Response Body: application/json

名称 类型 说明
access_token string server 端接口的鉴权令牌
expires_in long 有效期(秒),一般为 1 小时

# 获取Access Token结果举例

# 正常返回

http status 200

{
  "access_token": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "token_type": "bearer",
  "expires_in": 3599,
  "scope": "read-write"
}

# 异常返回

  • 未传grant_type参数

    http status 400

    {
      "error": "invalid_request",
      "error_description": "Missing grant type"
    }
    
  • 未传client_id参数

    http status 401

    {
      "timestamp": "2023-02-16 15:41:04",
      "status": 401,
      "error": "Unauthorized",
      "message": "Unauthorized",
      "path": "/oauth/token"
    }
    
  • 未传client_secret参数

    http status 401

    {
      "error": "invalid_client",
      "error_description": "Bad client credentials"
    }
    

# 访问受保护资源

访问受保护资源时,将access_token作为URL参数即可。

举例:

/somepath/**?access_token=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

# 异常返回

  • 未传access_token

    http status 401

    {
      "error": "unauthorized",
      "error_description": "Full authentication is required to access this resource"
    }
    
  • access_token校验失败

    http status 401

    {
      "error": "invalid_token",
      "error_description": "Invalid access token: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    }
    

其他异常,如token过期、无权限等,返回提示类似。

Access Token过期重新申请即可。