---
url: "https://pay.weixin.qq.com/doc/v3/partner/4017410365-go.md"
title: "获得品牌已授权且在生效中的产品权限信息-请求示例-go"
doc_id: 4017410365
example_lang: "go"
---

## Go


需配合微信支付工具库 wxpay\_utility 使用，请参考 [Go](https://pay.weixin.qq.com/doc/v3/partner/4015119446.md)

```
package main

import (
	"demo/wxpay_utility" // 引用微信支付工具库，参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
	"strings"
	"time"
)

func main() {
	// TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
	config, err := wxpay_utility.CreateMchConfig(
		"19xxxxxxxx",                 // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340
		"1DDE55AD98Exxxxxxxxxx",      // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924
		"/path/to/apiclient_key.pem", // 商户API证书私钥文件路径，本地文件路径
		"PUB_KEY_ID_xxxxxxxxxxxxx",   // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589
		"/path/to/wxp_pub.pem",       // 微信支付公钥文件路径，本地文件路径
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	request := &GetBrandProductsRequest{
		BrandId: wxpay_utility.String("BrandId_example"),
	}

	response, err := GetBrandProducts(config, request)
	if err != nil {
		fmt.Printf("请求失败: %+v\n", err)
		// TODO: 请求失败，根据状态码执行不同的处理
		return
	}

	// TODO: 请求成功，继续业务逻辑
	fmt.Printf("请求成功: %+v\n", response)
}

func GetBrandProducts(config *wxpay_utility.MchConfig, request *GetBrandProductsRequest) (response *GetBrandProductsResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "GET"
		path   = "/v3/brand-basic/partner/brands/{brand_id}/product-authorities"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqUrl.Path = strings.Replace(reqUrl.Path, "{brand_id}", url.PathEscape(*request.BrandId), -1)
	httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Accept", "application/json")
	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
	authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Authorization", authorization)

	client := &http.Client{}
	httpResponse, err := client.Do(httpRequest)
	if err != nil {
		return nil, err
	}
	respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
	if err != nil {
		return nil, err
	}
	if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
		// 2XX 成功，验证应答签名
		err = wxpay_utility.ValidateResponse(
			config.WechatPayPublicKeyId(),
			config.WechatPayPublicKey(),
			&httpResponse.Header,
			respBody,
		)
		if err != nil {
			return nil, err
		}
		response := &GetBrandProductsResponse{}
		if err := json.Unmarshal(respBody, response); err != nil {
			return nil, err
		}

		return response, nil
	} else {
		return nil, wxpay_utility.NewApiException(
			httpResponse.StatusCode,
			httpResponse.Header,
			respBody,
		)
	}
}

type GetBrandProductsRequest struct {
	BrandId *string `json:"brand_id,omitempty"`
}

func (o *GetBrandProductsRequest) MarshalJSON() ([]byte, error) {
	type Alias GetBrandProductsRequest
	a := &struct {
		BrandId *string `json:"brand_id,omitempty"`
		*Alias
	}{
		// 序列化时移除非 Body 字段
		BrandId: nil,
		Alias:   (*Alias)(o),
	}
	return json.Marshal(a)
}

type GetBrandProductsResponse struct {
	BrandId              *string                 `json:"brand_id,omitempty"`
	AuthorizationType    *AuthorizationType      `json:"authorization_type,omitempty"`
	AuthorizationProduct []BrandProductAuthority `json:"authorization_product,omitempty"`
	UpdateTime           *time.Time              `json:"update_time,omitempty"`
}

type AuthorizationType string

func (e AuthorizationType) Ptr() *AuthorizationType {
	return &e
}

const (
	AUTHORIZATIONTYPE_AUTHORIZATION_TYPE_HOSTING AuthorizationType = "AUTHORIZATION_TYPE_HOSTING"
	AUTHORIZATIONTYPE_AUTHORIZATION_TYPE_PARTLY  AuthorizationType = "AUTHORIZATION_TYPE_PARTLY"
)

type BrandProductAuthority string

func (e BrandProductAuthority) Ptr() *BrandProductAuthority {
	return &e
}

const (
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_MERCHANT_CARD           BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_MERCHANT_CARD"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_CARD_LINK               BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_CARD_LINK"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_PRODUCT_COUPON          BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_PRODUCT_COUPON"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_SHAKE_COUPON            BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_SHAKE_COUPON"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_BRAND_PAY_GIFT          BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_BRAND_PAY_GIFT"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_BRAND_STORE             BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_BRAND_STORE"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_PAYMENT_MERCHANT_MANAGE BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_PAYMENT_MERCHANT_MANAGE"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_APPID_MANAGE            BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_APPID_MANAGE"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_FINDER_MANAGE           BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_FINDER_MANAGE"
	BRANDPRODUCTAUTHORITY_BRAND_PRODUCT_AUTHORITY_CATEGORY_MANAGE         BrandProductAuthority = "BRAND_PRODUCT_AUTHORITY_CATEGORY_MANAGE"
)

```
