跳转到内容

授权 OAuth 应用程序

您可以让组织成员授权您的 OAuth 应用程序。

当您构建 OAuth 应用程序时,请实现下面描述的 Web 应用程序流程以获取授权码,然后将其交换为令牌。

您应该使用以下 GET 参数将用户重定向到 /oauth/authorize 端点:

Terminal window
https://accounts.crowdin.com/oauth/authorize

这将要求用户根据 REQUESTED_SCOPES 中指定的范围批准应用访问其账户,然后重定向回您在创建应用时提供的 REDIRECT_URI

名称描述
client_idstring必填。 注册应用程序时,您将收到该应用程序的客户端 ID。
redirect_uristring必填。 用户获得授权后被发送到的应用程序中的 URL。
response_type:codestring必填。 该参数用于 OAuth 应用程序的流程规范。
scopestring必填。 从可用作用域列表中选择您的应用所需的访问权限。 您可以添加多个以空格分隔的权限范围(无需使用引号)。
statestring强烈建议。 不可猜测的随机字符串。 使用它来提供额外的保护,抵御跨站请求伪造攻击。
code_challengestring强烈建议。 code_verifier 的 Base64-URL 编码 SHA-256 哈希值。 用于通过 PKCE 保护授权流程。 如果提供了 code_challenge_method,则为必填项。 详细了解 PKCE RFC
code_challenge_methodstring强烈建议。 用于派生 code_challenge 的方法。 Crowdin 支持 S256plain。 如果提供了 code_challenge,则为必填项。

将创建以下授权 URL:

Terminal window
https://accounts.crowdin.com/oauth/authorize?client_id=m50YenPpqac8u5D4dnK&redirect_uri=https://impact-mobile.com/auth/crowdin&response_type=code&scope=project+tm&state=d131dd02c5e6eec4

授权成功后,用户将被重定向回您的网站:

Terminal window
https://impact-mobile.com/auth/crowdin/?code=def50200df1fbb5ebac05f9288850d9e...0835bd3cf42&state=d131dd02c5e6eec4

如果授权被拒绝,用户将被重定向到您的网站并出现错误:

Terminal window
https://impact-mobile.com/auth/crowdin/?error=access_denied&state=d131dd02c5e6eec4

如果用户授权该应用程序,Crowdin 会重定向回您的站点,您可以用收到的代码交换访问令牌:

Terminal window
POST https://accounts.crowdin.com/oauth/token
名称描述
grant_type: authorization_codestring必填。 该参数用于 OAuth 应用程序的流程规范。
client_idstring必填。 注册应用程序时,您将收到该应用程序的客户端 ID。
client_secretstring必填。 当您注册该应用程序时,您会收到其客户端密钥。 如果您使用 PKCE(即提供 code_verifier),则不需要此参数。
redirect_uristring必填。 用户获得授权后被发送到的应用程序中的 URL。
codestring必填。 从回调查询字符串接收的代码。
code_verifierstring强烈建议。 用于生成 code_challenge 的原始随机字符串。 如果在初始授权请求中发送了 code_challenge,则为必填项。

例如,curl 中的请求采用以下形式:

Terminal window
curl -X POST \
https://accounts.crowdin.com/oauth/token \
-H "content-type: application/json" \
-d "{
\"grant_type\":\"authorization_code\",
\"client_id\":\"m50YenPpqac8u5D4dnK\",
\"client_secret\":\"yz35kYtjox...YE9Am\",
\"redirect_uri\":\"https://impact-mobile.com/auth/crowdin\",
\"code\":\"def50200df1fbb5ebac05f9288850d9e...0835bd3cf42\"
}"

默认情况下,响应采用以下形式:

{
"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJS...lag1e_Zk4EdJ5diYfz0",
"token_type":"bearer",
"expires_in": 7200,
"refresh_token": "b213c684ccaa7db1217e946e6ad...fff7ae"
}

访问令牌现在允许您以已授权用户的身份向 Crowdin API 发送请求。

例如,在 curl 中您可以设置以下授权标头:

Terminal window
curl -H "Authorization: Bearer ACCESS_TOKEN" https://api.crowdin.com/api/v2/projects

Crowdin Enterprise:

Terminal window
curl -H "Authorization: Bearer ACCESS_TOKEN" https://<organization_domain>.api.crowdin.com/api/v2/projects

详细了解 JWT 令牌结构

用户授权应用程序后收到的访问令牌具有过期时间。 访问令牌将在响应中定义的秒数后过期。

要刷新令牌而不需要用户重定向,请向授权服务器发送带有以下主体参数的 POST 请求:

Terminal window
POST https://accounts.crowdin.com/oauth/token
名称描述
grant_type: refresh_tokenstring必填。 该参数用于 OAuth 应用程序的流程规范。
client_idstring必填。 注册应用程序时,您将收到该应用程序的客户端 ID。
client_secretstring必填。 注册应用程序时,您将收到该应用程序的客户端密钥。
refresh_tokenstring必填。 从上次授权响应收到的刷新令牌。

例如,curl 中的请求采用以下形式:

Terminal window
curl -X POST \
https://accounts.crowdin.com/oauth/token \
-H "content-type: application/json" \
-d "{
\"grant_type\":\"refresh_token\",
\"client_id\":\"m50YenPpqac8u5D4dnK\",
\"client_secret\":\"yz35kYtjox...YE9Am\",
\"refresh_token\":\"b213c684ccaa7db1217e946e6ad...fff7ae\"
}"

默认情况下,响应采用以下形式:

{
"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJS...ZjFkMWI4OWFlIiwiaWF",
"token_type":"bearer",
"expires_in": 7200,
"refresh_token": "ea506ea4c37aa152f0a91ed2482...4a0c567"
}

当您在 Crowdin 上创建 OAuth 应用程序时,您可以注册一个或多个重定向 URL。

出于安全原因,如果该 URL 未包含在应用程序信息中,则授权后您将无法将用户重定向到此 URL。

本页面对你有帮助吗?