Authing 知识库
🛠 开发资源🥂 集成案例🔭 常见问题🖥 控制台
1.0.0
1.0.0
  • 欢迎
  • Authing 概述
  • 快速开始
    • 第一个应用
    • 基础概念
    • 实现单点登录
    • 控制台概览
    • 部署模型
  • 进阶指南
    • 理解认证和授权
      • 用户名 - 密码认证
      • 邮箱 - 密码认证
      • 手机 - 验证码认证
      • JWT Token 释义及使用
      • 验证 JWT Token
    • 配置用户权限
    • 最佳开发实践
    • 接入小程序扫码登录
      • 接入私有化小程序
    • 接入社会化登录
    • 接入 OAuth 2.0
      • 创建 OAuth 应用
      • 使用 OAuth 授权
    • 接入 OpenID Connect
      • 创建 OIDC 应用
      • 使用 OIDC 授权
      • 理解 OIDC 流程
      • OIDC 常见问题
    • 配置 LDAP 服务
    • 使用 Authing 的 LDAP 用户目录
    • 接入 SAML
      • 创建 SAML Identity Provider 应用
      • 创建 SAML Service Provider 应用
      • 理解 SAML 流程
      • 同时使用 Authing 作为 SP 和 IdP
      • 使用 SAML Identity Provider
        • 在阿里云访问管理中使用
        • 在腾讯云访问管理中使用
        • 在 Auth0 中使用
      • 使用 SAML Service Provider
        • 与 Auth0 SAML IdP 对接
        • 与 SSOCircle SAML IdP 对接
    • 使用 Webhook
    • 错误代码
  • 开发资源
    • 开发资源
    • API(GraphQL)
    • Guard for Web
    • Guard VS 自定义 UI
    • SDK for Web
      • 读取/修改用户权限
      • 绑定社会化登录
      • 管理 MFA 口令
      • 自定义请求链接
    • SDK for 微信小程序
    • SDK for Java
    • SDK for Objective-C
    • SDK for Python
    • SDK for Go
    • SDK for PHP
    • 函数计算(FaaS)
  • 通信
    • 邮件
    • SMS
  • MFA
    • 配置 MFA 安全口令
    • 接入 MFA
  • 安全
    • 配置 Web 安全域
    • 配置用户池密码强度
    • 配置密码加密函数
  • 其他
    • 常见问题
    • 集成案例
      • 使用 Authing 补充 AWS 中国区 Cognito User Pool
    • 社交互联数据
      • Authing 与社交互联数据
    • 为 Authing 贡献 SDK
      • 了解 Authing 的模块
Powered by GitBook
On this page
  • 安装
  • 开始使用
  • API 使用实例
  • User Endpoint
  • 注册一个新用户
  • 用户登录
  • 检查登录状态
  • 查询用户信息
  • 查询所有用户
  • 删除用户
  • 更新用户资料
  • 发送邮箱验证邮件
  • 发送重置密码邮件
  • 验证重置密码的验证码
  • 修改密码
  • OAuth Endpoint
  • 读取 OAuth 列表

Was this helpful?

  1. 开发资源

SDK for Go

PreviousSDK for PythonNextSDK for PHP

Last updated 5 years ago

Was this helpful?

Authing Go SDK 目前支持 Golang 1.8+ 版本。

GitHub 地址:。

安装

go get github.com/Authing/authing-go-sdk

开始使用

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "os"
    "regexp"

    authing "github.com/Authing/authing-go-sdk"
    prettyjson "github.com/hokaccha/go-prettyjson"
    "github.com/kelvinji2009/graphql"
)

const (
    clientID  = "5adb75e03055230001023b26"
    appSecret = "e683d18f9d597317d43d7a6522615b9d"
)

func main() {
    // ---User Endpoint
    client := authing.NewClient(clientID, appSecret, false)
    // Enable debug info for graphql client, just comment it if you want to disable the debug info
    client.Client.Log = func(s string) {
        b := []byte(s)
        pj, _ := prettyjson.Format(b)
        fmt.Println(string(pj))
    }

    // >>>Graphql Mutation: register
    input := authing.UserRegisterInput{
        Email:            graphql.String("kelvinji2009@gmail.com"),
        Password:         graphql.String("password"),
        RegisterInClient: graphql.String(clientID),
    }

    m, err := client.Register(&input)
    if err != nil {
        log.Println(">>>>Register failed: " + err.Error())
    } else {
        printJSON(m)
    }

    // ---OAuth Endpoint
    oauthClient := authing.NewOauthClient(clientID, appSecret, false)
    // Enable debug info for graphql client, just comment it if you want to disable the debug info
    oauthClient.Client.Log = func(s string) {
        b := []byte(s)
        pj, _ := prettyjson.Format(b)
        fmt.Println(string(pj))
    }

    // >>>>Graphql Query: Read OAuth List
    readOauthListQueryParameter := authing.ReadOauthListQueryParameter{
        ClientID:   graphql.String(clientID),
        DontGetURL: graphql.Boolean(false),
    }

    q, err := oauthClient.ReadOauthList(&readOauthListQueryParameter)
    if err != nil {
        log.Println(">>>>Read OAuth List failed: " + err.Error())
    } else {
        printJSON(q)
    }
}

// printJSON prints v as JSON encoded with indent to stdout. It panics on any error.
func printJSON(v interface{}) {
    w := json.NewEncoder(os.Stdout)
    w.SetIndent("", "\t")
    err := w.Encode(v)
    if err != nil {
        panic(err)
    }
}

API 使用实例

User Endpoint

请先创建一个用户 Endpoint Client。然后你可以对用户进行一系列操作,包括注册,登录,更新用户资料,删除用户,修改密码等等。

client := authing.NewClient(clientID, appSecret, false)
// Enable debug info for graphql client, just comment it if you want to disable the debug info
client.Client.Log = func(s string) { log.Println(s) }

注册一个新用户

input := authing.UserRegisterInput{
    Email:            graphql.String("kelvinji2009@gmail.com"),
    Password:         graphql.String("password"),
    RegisterInClient: graphql.String(clientID),
}

m, err := client.Register(&input)
if err != nil {
    log.Println(">>>>Register failed: " + err.Error())
} else {
    printJSON(m)
}

用户登录

loginInput := authing.UserLoginInput{
    Email:            graphql.String("kelvinji2009@gmail.com"),
    Password:         graphql.String("password!"),
    RegisterInClient: graphql.String(clientID),
}

m, err := client.Login(&loginInput)
if err != nil {
    log.Println(">>>>Login failed: " + err.Error())
} else {
    printJSON(m)
}

userID := string(m.Login.ID)

检查登录状态

q, err := client.CheckLoginStatus()
if err != nil {
    log.Println(">>>>Check login status failed: " + err.Error())
} else {
    printJSON(q)
}

查询用户信息

p := authing.UserQueryParameter{
    ID:               graphql.String("5ae3d830f0db4b000117a95e"),
    RegisterInClient: graphql.String(clientID),
}

q, err := client.User(&p)
if err != nil {
    log.Println(">>>>Query user failed: " + err.Error())
} else {
    printJSON(q)
}

查询所有用户

p := authing.UsersQueryParameter{
    RegisterInClient: graphql.String(clientID),
    Page:             graphql.Int(1),
    Count:            graphql.Int(10),
}

q, err := client.Users(&p)
if err != nil {
    log.Println(">>>>Query users failed: " + err.Error())
} else {
    printJSON(q)
}

删除用户

removeUsersInput := authing.RemoveUsersInput{
    IDs:              []graphql.String{"111", "222"}, // NOTE: Please use your real user IDs
    RegisterInClient: graphql.String(clientID),
    // Operator should be your `Authing.cn` account ID
    // Operator:         graphql.String("5adb75be3055230001023b20"), // no more needed
}

// UserID Validation
for i, id := range removeUsersInput.IDs {
    re := regexp.MustCompile("^[0-9a-fA-F]{24}$")

    if !re.MatchString(string(id)) {
        log.Fatalf(">>>> user ID is invalid ,index: %d, id: %s", i, id)
    }
}

m, err := client.RemoveUsers(&removeUsersInput)
if err != nil {
    log.Println(">>>>Remove users failed: " + err.Error())
} else {
    printJSON(m)
}

更新用户资料

userUpdateInput := authing.UserUpdateInput{
    ID:               graphql.String("5ae3d830f0db4b000117a95e"), // Mandotory in struct
    Username:         graphql.String("kelvinji2009x"),
    Nickname:         graphql.String("Sicario13th"),
    Phone:            graphql.String("18665308994"),
    RegisterInClient: graphql.String(clientID),
}

m, err := client.UpdateUser(&userUpdateInput)
if err != nil {
    log.Println(">>>>Update user failed: " + err.Error())
} else {
    printJSON(m)
}

发送邮箱验证邮件

sendVerifyEmailInput := authing.SendVerifyEmailInput{
    Email:  graphql.String("kelvinji2009@gmail.com"),
    Client: graphql.String(clientID),
}

err := client.SendVerifyEmail(&sendVerifyEmailInput)
if err != nil {
    log.Println(">>>>Send verify email failed: " + err.Error())
}

发送重置密码邮件

sendResetPasswordEmailInput := authing.SendResetPasswordEmailInput{
    Client: graphql.String(clientID),
    Email:  graphql.String("kelvinji2009@gmail.com"),
}

err := client.SendResetPasswordEmail(&sendResetPasswordEmailInput)
if err != nil {
    log.Println(">>>>Send reset password email failed: " + err.Error())
}

验证重置密码的验证码

verifyResetPasswordVerifyCodeInput := authing.VerifyResetPasswordVerifyCodeInput{
    Client:     graphql.String(clientID),
    Email:      graphql.String("kelvinji2009@gmail.com"),
    VerifyCode: graphql.String("7670"),
}

err := client.VerifyResetPasswordVerifyCode(&verifyResetPasswordVerifyCodeInput)
if err != nil {
    log.Println(">>>>Verify reset passwod verify code failed: " + err.Error())
}

修改密码

changePasswordInput := authing.ChangePasswordInput{
    Client:     graphql.String(clientID),
    Email:      graphql.String("kelvinji2009@gmail.com"),
    VerifyCode: graphql.String("7670"),
    Password:   graphql.String("password!"),
}

err := client.ChangePassword(&changePasswordInput)
if err != nil {
    log.Println(">>>>Change password failed: " + err.Error())
}

OAuth Endpoint

请先创建 OAuth Endpoint Client.

oauthClient := authing.NewOauthClient(clientID, appSecret, false)
// Enable debug info for graphql client, just comment it if you want to disable the debug info
oauthClient.Client.Log = func(s string) { log.Println(s) }

读取 OAuth 列表

readOauthListQueryParameter := authing.ReadOauthListQueryParameter{
    ClientID:   graphql.String(clientID),
    DontGetURL: graphql.Boolean(false),
}

q, err := oauthClient.ReadOauthList(&readOauthListQueryParameter)
if err != nil {
    log.Println(">>>>Read OAuth List failed: " + err.Error())
} else {
    printJSON(q)
}

https://github.com/Authing/authing-go-sdk
如何获取 Client ID 和 Client Secret ?