telnyx-oauth-go
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
<!-- Auto-generated from Telnyx OpenAPI specs. Do not edit. -->
Telnyx Oauth - Go
Telnyx Oauth - Go
Installation
安装
bash
go get github.com/team-telnyx/telnyx-gobash
go get github.com/team-telnyx/telnyx-goSetup
初始化配置
go
import (
"context"
"fmt"
"os"
"github.com/team-telnyx/telnyx-go"
"github.com/team-telnyx/telnyx-go/option"
)
client := telnyx.NewClient(
option.WithAPIKey(os.Getenv("TELNYX_API_KEY")),
)All examples below assume is already initialized as shown above.
clientgo
import (
"context"
"fmt"
"os"
"github.com/team-telnyx/telnyx-go"
"github.com/team-telnyx/telnyx-go/option"
)
client := telnyx.NewClient(
option.WithAPIKey(os.Getenv("TELNYX_API_KEY")),
)以下所有示例都默认已经按照上述方式完成初始化。
clientError Handling
错误处理
All API calls can fail with network errors, rate limits (429), validation errors (422),
or authentication errors (401). Always handle errors in production code:
go
import "errors"
result, err := client.Messages.Send(ctx, params)
if err != nil {
var apiErr *telnyx.Error
if errors.As(err, &apiErr) {
switch apiErr.StatusCode {
case 422:
fmt.Println("Validation error — check required fields and formats")
case 429:
// Rate limited — wait and retry with exponential backoff
fmt.Println("Rate limited, retrying...")
default:
fmt.Printf("API error %d: %s\n", apiErr.StatusCode, apiErr.Error())
}
} else {
fmt.Println("Network error — check connectivity and retry")
}
}Common error codes: invalid API key, insufficient permissions,
resource not found, validation error (check field formats),
rate limited (retry with exponential backoff).
401403404422429所有API调用都可能失败,原因包括网络错误、速率限制(429)、校验错误(422)、或认证错误(401)。在生产环境代码中请务必处理错误:
go
import "errors"
result, err := client.Messages.Send(ctx, params)
if err != nil {
var apiErr *telnyx.Error
if errors.As(err, &apiErr) {
switch apiErr.StatusCode {
case 422:
fmt.Println("Validation error — check required fields and formats")
case 429:
// Rate limited — wait and retry with exponential backoff
fmt.Println("Rate limited, retrying...")
default:
fmt.Printf("API error %d: %s\n", apiErr.StatusCode, apiErr.Error())
}
} else {
fmt.Println("Network error — check connectivity and retry")
}
}常见错误码: 无效API密钥, 权限不足, 资源不存在, 校验错误(请检查字段格式), 触发速率限制(使用指数退避策略重试)。
401403404422429Important Notes
重要说明
- Pagination: Use for automatic iteration:
ListAutoPaging().iter := client.Resource.ListAutoPaging(ctx, params); for iter.Next() { item := iter.Current() }
- 分页: 使用进行自动遍历:
ListAutoPaging()。iter := client.Resource.ListAutoPaging(ctx, params); for iter.Next() { item := iter.Current() }
Authorization server metadata
授权服务器元数据
OAuth 2.0 Authorization Server Metadata (RFC 8414)
GET /.well-known/oauth-authorization-servergo
response, err := client.WellKnown.GetAuthorizationServerMetadata(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AuthorizationEndpoint)Returns: (uri), (array[string]), (array[string]), (uri), (uri), (uri), (uri), (array[string]), (array[string]), (uri), (array[string])
authorization_endpointcode_challenge_methods_supportedgrant_types_supportedintrospection_endpointissuerjwks_uriregistration_endpointresponse_types_supportedscopes_supportedtoken_endpointtoken_endpoint_auth_methods_supportedOAuth 2.0授权服务器元数据(RFC 8414)
GET /.well-known/oauth-authorization-servergo
response, err := client.WellKnown.GetAuthorizationServerMetadata(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AuthorizationEndpoint)返回值:(uri类型),(字符串数组类型),(字符串数组类型),(uri类型),(uri类型),(uri类型),(uri类型),(字符串数组类型),(字符串数组类型),(uri类型),(字符串数组类型)
authorization_endpointcode_challenge_methods_supportedgrant_types_supportedintrospection_endpointissuerjwks_uriregistration_endpointresponse_types_supportedscopes_supportedtoken_endpointtoken_endpoint_auth_methods_supportedProtected resource metadata
受保护资源元数据
OAuth 2.0 Protected Resource Metadata for resource discovery
GET /.well-known/oauth-protected-resourcego
response, err := client.WellKnown.GetProtectedResourceMetadata(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AuthorizationServers)Returns: (array[string]), (uri)
authorization_serversresource用于资源发现的OAuth 2.0受保护资源元数据
GET /.well-known/oauth-protected-resourcego
response, err := client.WellKnown.GetProtectedResourceMetadata(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AuthorizationServers)返回值:(字符串数组类型),(uri类型)
authorization_serversresourceOAuth authorization endpoint
OAuth授权端点
OAuth 2.0 authorization endpoint for the authorization code flow
GET /oauth/authorizego
err := client.OAuth.GetAuthorize(context.Background(), telnyx.OAuthGetAuthorizeParams{
ClientID: "550e8400-e29b-41d4-a716-446655440000",
RedirectUri: "https://example.com",
ResponseType: telnyx.OAuthGetAuthorizeParamsResponseTypeCode,
})
if err != nil {
log.Fatal(err)
}用于授权码流程的OAuth 2.0授权端点
GET /oauth/authorizego
err := client.OAuth.GetAuthorize(context.Background(), telnyx.OAuthGetAuthorizeParams{
ClientID: "550e8400-e29b-41d4-a716-446655440000",
RedirectUri: "https://example.com",
ResponseType: telnyx.OAuthGetAuthorizeParamsResponseTypeCode,
})
if err != nil {
log.Fatal(err)
}Get OAuth consent token
获取OAuth consent token
Retrieve details about an OAuth consent token
GET /oauth/consent/{consent_token}go
oauth, err := client.OAuth.Get(context.Background(), "consent_token")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauth.Data)Returns: (string), (uri), (string), (uri), (uri), (array[object]), (uri), (boolean)
client_idlogo_urinamepolicy_uriredirect_urirequested_scopestos_uriverified获取OAuth consent token的详细信息
GET /oauth/consent/{consent_token}go
oauth, err := client.OAuth.Get(context.Background(), "consent_token")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauth.Data)返回值:(字符串类型),(uri类型),(字符串类型),(uri类型),(uri类型),(对象数组类型),(uri类型),(布尔类型)
client_idlogo_urinamepolicy_uriredirect_urirequested_scopestos_uriverifiedCreate OAuth grant
创建OAuth授权许可
Create an OAuth authorization grant
POST /oauth/grantsallowedconsent_tokengo
response, err := client.OAuth.Grants(context.Background(), telnyx.OAuthGrantsParams{
Allowed: true,
ConsentToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.example",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.RedirectUri)Returns: (uri)
redirect_uri创建OAuth授权许可
POST /oauth/grantsallowedconsent_tokengo
response, err := client.OAuth.Grants(context.Background(), telnyx.OAuthGrantsParams{
Allowed: true,
ConsentToken: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.example",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.RedirectUri)返回值:(uri类型)
redirect_uriToken introspection
Token自省
Introspect an OAuth access token to check its validity and metadata
POST /oauth/introspecttokengo
response, err := client.OAuth.Introspect(context.Background(), telnyx.OAuthIntrospectParams{
Token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.example",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.ClientID)Returns: (boolean), (string), (string), (integer), (integer), (string), (string)
activeaudclient_idexpiatissscope校验OAuth访问令牌的有效性并获取其元数据
POST /oauth/introspecttokengo
response, err := client.OAuth.Introspect(context.Background(), telnyx.OAuthIntrospectParams{
Token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.example",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.ClientID)返回值:(布尔类型),(字符串类型),(字符串类型),(整数类型),(整数类型),(字符串类型),(字符串类型)
activeaudclient_idexpiatissscopeJSON Web Key Set
JSON Web Key Set
Retrieve the JSON Web Key Set for token verification
GET /oauth/jwksgo
response, err := client.OAuth.GetJwks(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Keys)Returns: (array[object])
keys获取用于令牌校验的JSON Web Key Set
GET /oauth/jwksgo
response, err := client.OAuth.GetJwks(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.Keys)返回值:(对象数组类型)
keysDynamic client registration
动态客户端注册
Register a new OAuth client dynamically (RFC 7591)
POST /oauth/registerOptional: (string), (array[string]), (uri), (uri), (array[string]), (array[string]), (string), (enum: none, client_secret_basic, client_secret_post), (uri)
client_namegrant_typeslogo_uripolicy_uriredirect_urisresponse_typesscopetoken_endpoint_auth_methodtos_urigo
response, err := client.OAuth.Register(context.Background(), telnyx.OAuthRegisterParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.ClientID)Returns: (string), (integer), (string), (string), (array[string]), (uri), (uri), (array[string]), (array[string]), (string), (string), (uri)
client_idclient_id_issued_atclient_nameclient_secretgrant_typeslogo_uripolicy_uriredirect_urisresponse_typesscopetoken_endpoint_auth_methodtos_uri动态注册新的OAuth客户端(RFC 7591)
POST /oauth/register可选参数:(字符串类型),(字符串数组类型),(uri类型),(uri类型),(字符串数组类型),(字符串数组类型),(字符串类型),(枚举值:none, client_secret_basic, client_secret_post),(uri类型)
client_namegrant_typeslogo_uripolicy_uriredirect_urisresponse_typesscopetoken_endpoint_auth_methodtos_urigo
response, err := client.OAuth.Register(context.Background(), telnyx.OAuthRegisterParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.ClientID)返回值:(字符串类型),(整数类型),(字符串类型),(字符串类型),(字符串数组类型),(uri类型),(uri类型),(字符串数组类型),(字符串数组类型),(字符串类型),(字符串类型),(uri类型)
client_idclient_id_issued_atclient_nameclient_secretgrant_typeslogo_uripolicy_uriredirect_urisresponse_typesscopetoken_endpoint_auth_methodtos_uriOAuth token endpoint
OAuth令牌端点
Exchange authorization code, client credentials, or refresh token for access token
POST /oauth/tokengrant_typeOptional: (string), (string), (string), (string), (uri), (string), (string)
client_idclient_secretcodecode_verifierredirect_urirefresh_tokenscopego
response, err := client.OAuth.Token(context.Background(), telnyx.OAuthTokenParams{
GrantType: telnyx.OAuthTokenParamsGrantTypeClientCredentials,
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AccessToken)Returns: (string), (integer), (string), (string), (enum: Bearer)
access_tokenexpires_inrefresh_tokenscopetoken_type使用授权码、客户端凭证或者刷新令牌兑换访问令牌
POST /oauth/tokengrant_type可选参数:(字符串类型),(字符串类型),(字符串类型),(字符串类型),(uri类型),(字符串类型),(字符串类型)
client_idclient_secretcodecode_verifierredirect_urirefresh_tokenscopego
response, err := client.OAuth.Token(context.Background(), telnyx.OAuthTokenParams{
GrantType: telnyx.OAuthTokenParamsGrantTypeClientCredentials,
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", response.AccessToken)返回值:(字符串类型),(整数类型),(字符串类型),(字符串类型),(枚举值:Bearer)
access_tokenexpires_inrefresh_tokenscopetoken_typeList OAuth clients
列出OAuth客户端
Retrieve a paginated list of OAuth clients for the authenticated user
GET /oauth_clientsgo
page, err := client.OAuthClients.List(context.Background(), telnyx.OAuthClientListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)Returns: (array[string]), (array[string]), (string), (string | null), (enum: public, confidential), (date-time), (uri), (string), (string), (uri), (enum: oauth_client), (array[string]), (boolean), (uri), (date-time), (string)
allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_id获取当前认证用户的OAuth客户端分页列表
GET /oauth_clientsgo
page, err := client.OAuthClients.List(context.Background(), telnyx.OAuthClientListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)返回值:(字符串数组类型),(字符串数组类型),(字符串类型),(字符串/空类型),(枚举值:public, confidential),(日期时间类型),(uri类型),(字符串类型),(字符串类型),(uri类型),(枚举值:oauth_client),(字符串数组类型),(布尔类型),(uri类型),(日期时间类型),(字符串类型)
allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_idCreate OAuth client
创建OAuth客户端
Create a new OAuth client
POST /oauth_clientsnameallowed_scopesclient_typeallowed_grant_typesOptional: (uri), (uri), (array[string]), (boolean), (uri)
logo_uripolicy_uriredirect_urisrequire_pkcetos_urigo
oauthClient, err := client.OAuthClients.New(context.Background(), telnyx.OAuthClientNewParams{
AllowedGrantTypes: []string{"client_credentials"},
AllowedScopes: []string{"admin"},
ClientType: telnyx.OAuthClientNewParamsClientTypePublic,
Name: "My OAuth client",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)Returns: (array[string]), (array[string]), (string), (string | null), (enum: public, confidential), (date-time), (uri), (string), (string), (uri), (enum: oauth_client), (array[string]), (boolean), (uri), (date-time), (string)
allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_id创建新的OAuth客户端
POST /oauth_clientsnameallowed_scopesclient_typeallowed_grant_types可选参数:(uri类型),(uri类型),(字符串数组类型),(布尔类型),(uri类型)
logo_uripolicy_uriredirect_urisrequire_pkcetos_urigo
oauthClient, err := client.OAuthClients.New(context.Background(), telnyx.OAuthClientNewParams{
AllowedGrantTypes: []string{"client_credentials"},
AllowedScopes: []string{"admin"},
ClientType: telnyx.OAuthClientNewParamsClientTypePublic,
Name: "My OAuth client",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)返回值:(字符串数组类型),(字符串数组类型),(字符串类型),(字符串/空类型),(枚举值:public, confidential),(日期时间类型),(uri类型),(字符串类型),(字符串类型),(uri类型),(枚举值:oauth_client),(字符串数组类型),(布尔类型),(uri类型),(日期时间类型),(字符串类型)
allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_idGet OAuth client
获取OAuth客户端
Retrieve a single OAuth client by ID
GET /oauth_clients/{id}go
oauthClient, err := client.OAuthClients.Get(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)Returns: (array[string]), (array[string]), (string), (string | null), (enum: public, confidential), (date-time), (uri), (string), (string), (uri), (enum: oauth_client), (array[string]), (boolean), (uri), (date-time), (string)
allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_id根据ID获取单个OAuth客户端信息
GET /oauth_clients/{id}go
oauthClient, err := client.OAuthClients.Get(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)返回值:(字符串数组类型),(字符串数组类型),(字符串类型),(字符串/空类型),(枚举值:public, confidential),(日期时间类型),(uri类型),(字符串类型),(字符串类型),(uri类型),(枚举值:oauth_client),(字符串数组类型),(布尔类型),(uri类型),(日期时间类型),(字符串类型)
allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_idUpdate OAuth client
更新OAuth客户端
Update an existing OAuth client
PUT /oauth_clients/{id}Optional: (array[string]), (array[string]), (uri), (string), (uri), (array[string]), (boolean), (uri)
allowed_grant_typesallowed_scopeslogo_urinamepolicy_uriredirect_urisrequire_pkcetos_urigo
oauthClient, err := client.OAuthClients.Update(
context.Background(),
"182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
telnyx.OAuthClientUpdateParams{},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)Returns: (array[string]), (array[string]), (string), (string | null), (enum: public, confidential), (date-time), (uri), (string), (string), (uri), (enum: oauth_client), (array[string]), (boolean), (uri), (date-time), (string)
allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_id更新已有的OAuth客户端
PUT /oauth_clients/{id}可选参数:(字符串数组类型),(字符串数组类型),(uri类型),(字符串类型),(uri类型),(字符串数组类型),(布尔类型),(uri类型)
allowed_grant_typesallowed_scopeslogo_urinamepolicy_uriredirect_urisrequire_pkcetos_urigo
oauthClient, err := client.OAuthClients.Update(
context.Background(),
"182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
telnyx.OAuthClientUpdateParams{},
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthClient.Data)返回值:(字符串数组类型),(字符串数组类型),(字符串类型),(字符串/空类型),(枚举值:public, confidential),(日期时间类型),(uri类型),(字符串类型),(字符串类型),(uri类型),(枚举值:oauth_client),(字符串数组类型),(布尔类型),(uri类型),(日期时间类型),(字符串类型)
allowed_grant_typesallowed_scopesclient_idclient_secretclient_typecreated_atlogo_urinameorg_idpolicy_urirecord_typeredirect_urisrequire_pkcetos_uriupdated_atuser_idDelete OAuth client
删除OAuth客户端
Delete an OAuth client
DELETE /oauth_clients/{id}go
err := client.OAuthClients.Delete(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}删除OAuth客户端
DELETE /oauth_clients/{id}go
err := client.OAuthClients.Delete(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}List OAuth grants
列出OAuth授权许可
Retrieve a paginated list of OAuth grants for the authenticated user
GET /oauth_grantsgo
page, err := client.OAuthGrants.List(context.Background(), telnyx.OAuthGrantListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)Returns: (string), (date-time), (uuid), (date-time), (enum: oauth_grant), (array[string])
client_idcreated_atidlast_used_atrecord_typescopes获取当前认证用户的OAuth授权许可分页列表
GET /oauth_grantsgo
page, err := client.OAuthGrants.List(context.Background(), telnyx.OAuthGrantListParams{})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", page)返回值:(字符串类型),(日期时间类型),(uuid类型),(日期时间类型),(枚举值:oauth_grant),(字符串数组类型)
client_idcreated_atidlast_used_atrecord_typescopesGet OAuth grant
获取OAuth授权许可
Retrieve a single OAuth grant by ID
GET /oauth_grants/{id}go
oauthGrant, err := client.OAuthGrants.Get(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthGrant.Data)Returns: (string), (date-time), (uuid), (date-time), (enum: oauth_grant), (array[string])
client_idcreated_atidlast_used_atrecord_typescopes根据ID获取单个OAuth授权许可信息
GET /oauth_grants/{id}go
oauthGrant, err := client.OAuthGrants.Get(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthGrant.Data)返回值:(字符串类型),(日期时间类型),(uuid类型),(日期时间类型),(枚举值:oauth_grant),(字符串数组类型)
client_idcreated_atidlast_used_atrecord_typescopesRevoke OAuth grant
吊销OAuth授权许可
Revoke an OAuth grant
DELETE /oauth_grants/{id}go
oauthGrant, err := client.OAuthGrants.Delete(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthGrant.Data)Returns: (string), (date-time), (uuid), (date-time), (enum: oauth_grant), (array[string])
client_idcreated_atidlast_used_atrecord_typescopes吊销OAuth授权许可
DELETE /oauth_grants/{id}go
oauthGrant, err := client.OAuthGrants.Delete(context.Background(), "182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", oauthGrant.Data)返回值:(字符串类型),(日期时间类型),(uuid类型),(日期时间类型),(枚举值:oauth_grant),(字符串数组类型)
client_idcreated_atidlast_used_atrecord_typescopes