Documentation ¶
Index ¶
- Constants
- Variables
- func BuildKey(client *Client, tokenType TokenType) (string, error)
- func NewConfigBuilder() *configBuilder
- func WithFile(data *FileUpload) func(*requestOptions)
- func WithJSONData(data any) func(*requestOptions)
- func WithQuery(query netURL.Values) func(*requestOptions)
- func WithToken(token *TokenDescriptor) func(*requestOptions)
- type APIBaseResponse
- type AuthCorpAccessTokenResponse
- type Client
- func (c *Client) FetchAccessTokenIfNeeded(ctx context.Context) (tk string, err error)
- func (c *Client) FetchAuthCorpAccessTokenIfNeeded(ctx context.Context) (tk string, err error)
- func (c *Client) FetchProviderTokenIfNeeded(ctx context.Context) (tk string, err error)
- func (c *Client) FetchSuiteTokenIfNeeded(ctx context.Context) (tk string, err error)
- func (c *Client) GetAccessToken(ctx context.Context, corpid string, corpSecret string) (response GetTokenResponse, err error)
- func (c *Client) GetAuthCorpAccessToken(ctx context.Context, authCorpid, permanentCode string) (response AuthCorpAccessTokenResponse, err error)
- func (c *Client) GetConfig() *ClientConfig
- func (c *Client) GetInternalCorpConfig() *InternalCorp
- func (c *Client) GetOpenCorpConfig() *OpenCorp
- func (c *Client) GetProviderToken(ctx context.Context, corpid string, providerSecret string) (response GetProviderTokenResponse, err error)
- func (c *Client) GetStore() Store
- func (c *Client) GetSuiteToken(ctx context.Context, suiteId, suiteSecret, suiteTicket string) (response SuiteTokenResponse, err error)
- func (c *Client) GetToken(ctx context.Context, token *TokenDescriptor) (tk string, err error)
- func (c *Client) InitStoreData(data []StoreData, ctx context.Context) error
- func (c *Client) NewRequest(ctx context.Context, method string, url string, setters ...requestOption) (req *http.Request, err error)
- func (c *Client) Request(ctx context.Context, method string, url string, v interface{}, ...) error
- func (c *Client) SendRequest(req *http.Request, v interface{}) error
- func (c *Client) SetAccessToken(ctx context.Context, accessToken string, expiresIn time.Duration) (err error)
- func (c *Client) SetAuthCorpAccessToken(ctx context.Context, authCorpAccessToken string, expiresIn time.Duration) (err error)
- func (c *Client) SetAuthCorpID(authCorpID string)
- func (c *Client) SetPermanentCode(ctx context.Context, permanentCode string) (err error)
- func (c *Client) SetProviderAccessToken(ctx context.Context, providerAccessToken string, expiresIn time.Duration) (err error)
- func (c *Client) SetSuiteTicket(ctx context.Context, suiteTicket string) (err error)
- func (c *Client) SetSuiteToken(ctx context.Context, suiteAccessToken string, expiresIn time.Duration) (err error)
- type ClientConfig
- type FileUpload
- type GetProviderTokenResponse
- type GetTokenResponse
- type H
- type InternalCorp
- type MemoryStore
- type OpenCorp
- type Options
- type Store
- type StoreData
- type SuiteTokenResponse
- type TokenDescriptor
- type TokenInfo
- type TokenType
Constants ¶
const ( WeWorkAPIURL = "https://qyapi.weixin.qq.com/cgi-bin" Timeout = time.Second * 30 )
Variables ¶
var ( AccessToken = &TokenDescriptor{TokenType: "AccessToken", ParamValue: "access_token"} ProviderToken = &TokenDescriptor{TokenType: "ProviderAccessToken", ParamValue: "provider_access_token"} SuiteToken = &TokenDescriptor{TokenType: "SuiteAccessToken", ParamValue: "suite_access_token"} AuthCorpAccessToken = &TokenDescriptor{TokenType: "AuthCorpAccessToken", ParamValue: "access_token"} // 注意这里的区别 SuiteTicket = &TokenDescriptor{TokenType: "SuiteTicket", ParamValue: "suite_ticket"} PermanentCode = &TokenDescriptor{TokenType: "PermanentCode", ParamValue: "permanent_code"} )
var ErrInvalidInternalCorp = errors.New("invalid internal corp config")
var ErrInvalidOpenCorp = errors.New("invalid open corp config")
var ErrNilStoreToken = errors.New("store token is nil or expired")
Functions ¶
func NewConfigBuilder ¶
func NewConfigBuilder() *configBuilder
func WithFile ¶
func WithFile(data *FileUpload) func(*requestOptions)
func WithJSONData ¶
func WithJSONData(data any) func(*requestOptions)
func WithToken ¶
func WithToken(token *TokenDescriptor) func(*requestOptions)
Types ¶
type APIBaseResponse ¶
type AuthCorpAccessTokenResponse ¶
type AuthCorpAccessTokenResponse struct { APIBaseResponse AccessToken string `json:"access_token"` ExpiresIn int64 `json:"expires_in"` }
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(config *ClientConfig) *Client
func (*Client) FetchAccessTokenIfNeeded ¶
从缓存中获取AccessToken(内部应用),如果缓存中没有,则从企微API获取
func (*Client) FetchAuthCorpAccessTokenIfNeeded ¶
从缓存中获取AccessToken(第三方授权应用),如果缓存中没有,则从企微API获取
func (*Client) FetchProviderTokenIfNeeded ¶
获取服务商凭证,如果缓存中没有,则从企微API获取
func (*Client) FetchSuiteTokenIfNeeded ¶
获取第三方应用凭证,如果缓存中没有,则从企微API获取
func (*Client) GetAccessToken ¶
func (c *Client) GetAccessToken(ctx context.Context, corpid string, corpSecret string) (response GetTokenResponse, err error)
内部应用获取access_token
IMPORTANT: 此方法不会缓存获取到的凭证,需要自行缓存!建议使用GetToken或FetchAccessTokenIfNeeded方法进行获取
获取access_token是调用企业微信API接口的第一步,相当于创建了一个登录凭证,其它的业务API接口,都需要依赖于access_token来鉴权调用者身份。 因此开发者,在使用业务接口前,要明确access_token的颁发来源,使用正确的access_token。
文档地址: https://developer.work.weixin.qq.com/document/path/91039
func (*Client) GetAuthCorpAccessToken ¶
func (c *Client) GetAuthCorpAccessToken(ctx context.Context, authCorpid, permanentCode string) (response AuthCorpAccessTokenResponse, err error)
第三方服务商获取授权企业的access_token
IMPORTANT: 此方法不会缓存获取到的凭证,需要自行缓存!建议使用GetToken或FetchAuthCorpAccessTokenIfNeeded方法进行获取
第三方服务商在取得企业的永久授权码后,通过此接口可以获取到企业的access_token。 获取后可通过通讯录、应用、消息等企业接口来运营这些应用。
文档地址: https://developer.work.weixin.qq.com/document/path/90605
- 此处获得的企业access_token与企业获取access_token拿到的token,本质上是一样的,只不过获取方式不同。获取之后,就跟普通企业一样使用token调用API接口
func (*Client) GetConfig ¶
func (c *Client) GetConfig() *ClientConfig
func (*Client) GetInternalCorpConfig ¶
func (c *Client) GetInternalCorpConfig() *InternalCorp
func (*Client) GetOpenCorpConfig ¶
func (*Client) GetProviderToken ¶
func (c *Client) GetProviderToken(ctx context.Context, corpid string, providerSecret string) (response GetProviderTokenResponse, err error)
获取服务商凭证
IMPORTANT: 此方法不会缓存获取到的凭证,需要自行缓存!建议使用GetToken或FetchProviderTokenIfNeeded方法进行获取
该API用于获取服务商凭证,该凭证用于服务商调用企业微信开放接口。
文档地址: https://developer.work.weixin.qq.com/document/path/91200
func (*Client) GetSuiteToken ¶
func (c *Client) GetSuiteToken(ctx context.Context, suiteId, suiteSecret, suiteTicket string) (response SuiteTokenResponse, err error)
获取第三方应用凭证
IMPORTANT: 此方法不会缓存获取到的凭证,需要自行缓存!建议使用GetToken或FetchSuiteTokenIfNeeded方法进行获取
该API用于获取第三方应用凭证(suite_access_token)。
文档地址: https://developer.work.weixin.qq.com/document/path/90600
- 由于第三方服务商可能托管了大量的企业,其安全问题造成的影响会更加严重,故API中除了合法来源IP校验之外,还额外增加了suite_ticket作为安全凭证。
- 获取suite_access_token时,需要suite_ticket参数。suite_ticket由企业微信后台定时推送给“指令回调URL”,每十分钟更新一次,见推送suite_ticket。
- suite_ticket实际有效期为30分钟,可以容错连续两次获取suite_ticket失败的情况,但是请永远使用最新接收到的suite_ticket。通过本接口获取的suite_access_token有效期为2小时,开发者需要进行缓存,不可频繁获取。
func (*Client) InitStoreData ¶
func (*Client) NewRequest ¶
func (*Client) SetAccessToken ¶
func (*Client) SetAuthCorpAccessToken ¶
func (*Client) SetAuthCorpID ¶
func (*Client) SetPermanentCode ¶
func (*Client) SetProviderAccessToken ¶
func (*Client) SetSuiteTicket ¶
type ClientConfig ¶
type GetProviderTokenResponse ¶
type GetProviderTokenResponse struct { APIBaseResponse ProviderAccessToken string `json:"provider_access_token"` ExpiresIn int64 `json:"expires_in"` }
type GetTokenResponse ¶
type GetTokenResponse struct { APIBaseResponse AccessToken string `json:"access_token"` ExpiresIn int64 `json:"expires_in"` }
type InternalCorp ¶
type MemoryStore ¶
type MemoryStore struct {
// contains filtered or unexported fields
}
func NewMemoryStore ¶
func NewMemoryStore() *MemoryStore
type Store ¶
type Store interface { GetToken(client *Client, ctx context.Context, tokenType TokenType) (string, error) SetToken(client *Client, ctx context.Context, tokenType TokenType, token string, expiresIn time.Duration) error }
func InitMemoryStore ¶
func InitMemoryStore() Store
type StoreData ¶
type StoreData struct { TokenDescriptor *TokenDescriptor Token string }
type SuiteTokenResponse ¶
type SuiteTokenResponse struct { APIBaseResponse SuiteAccessToken string `json:"suite_access_token"` ExpiresIn int64 `json:"expires_in"` }