github.com/twilio/twilio-go@v1.20.1/rest/oauth/v1/token.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Oauth
     8   * This is the public Twilio REST API.
     9   *
    10   * NOTE: This class is auto generated by OpenAPI Generator.
    11   * https://openapi-generator.tech
    12   * Do not edit the class manually.
    13   */
    14  
    15  package openapi
    16  
    17  import (
    18  	"encoding/json"
    19  	"net/url"
    20  )
    21  
    22  // Optional parameters for the method 'CreateToken'
    23  type CreateTokenParams struct {
    24  	// Grant type is a credential representing resource owner's authorization which can be used by client to obtain access token.
    25  	GrantType *string `json:"GrantType,omitempty"`
    26  	// A 34 character string that uniquely identifies this OAuth App.
    27  	ClientId *string `json:"ClientId,omitempty"`
    28  	// The credential for confidential OAuth App.
    29  	ClientSecret *string `json:"ClientSecret,omitempty"`
    30  	// JWT token related to the authorization code grant type.
    31  	Code *string `json:"Code,omitempty"`
    32  	// The redirect uri
    33  	RedirectUri *string `json:"RedirectUri,omitempty"`
    34  	// The targeted audience uri
    35  	Audience *string `json:"Audience,omitempty"`
    36  	// JWT token related to refresh access token.
    37  	RefreshToken *string `json:"RefreshToken,omitempty"`
    38  	// The scope of token
    39  	Scope *string `json:"Scope,omitempty"`
    40  }
    41  
    42  func (params *CreateTokenParams) SetGrantType(GrantType string) *CreateTokenParams {
    43  	params.GrantType = &GrantType
    44  	return params
    45  }
    46  func (params *CreateTokenParams) SetClientId(ClientId string) *CreateTokenParams {
    47  	params.ClientId = &ClientId
    48  	return params
    49  }
    50  func (params *CreateTokenParams) SetClientSecret(ClientSecret string) *CreateTokenParams {
    51  	params.ClientSecret = &ClientSecret
    52  	return params
    53  }
    54  func (params *CreateTokenParams) SetCode(Code string) *CreateTokenParams {
    55  	params.Code = &Code
    56  	return params
    57  }
    58  func (params *CreateTokenParams) SetRedirectUri(RedirectUri string) *CreateTokenParams {
    59  	params.RedirectUri = &RedirectUri
    60  	return params
    61  }
    62  func (params *CreateTokenParams) SetAudience(Audience string) *CreateTokenParams {
    63  	params.Audience = &Audience
    64  	return params
    65  }
    66  func (params *CreateTokenParams) SetRefreshToken(RefreshToken string) *CreateTokenParams {
    67  	params.RefreshToken = &RefreshToken
    68  	return params
    69  }
    70  func (params *CreateTokenParams) SetScope(Scope string) *CreateTokenParams {
    71  	params.Scope = &Scope
    72  	return params
    73  }
    74  
    75  // Issues a new Access token (optionally identity_token & refresh_token) in exchange of Oauth grant
    76  func (c *ApiService) CreateToken(params *CreateTokenParams) (*OauthV1Token, error) {
    77  	path := "/v1/token"
    78  
    79  	data := url.Values{}
    80  	headers := make(map[string]interface{})
    81  
    82  	if params != nil && params.GrantType != nil {
    83  		data.Set("GrantType", *params.GrantType)
    84  	}
    85  	if params != nil && params.ClientId != nil {
    86  		data.Set("ClientId", *params.ClientId)
    87  	}
    88  	if params != nil && params.ClientSecret != nil {
    89  		data.Set("ClientSecret", *params.ClientSecret)
    90  	}
    91  	if params != nil && params.Code != nil {
    92  		data.Set("Code", *params.Code)
    93  	}
    94  	if params != nil && params.RedirectUri != nil {
    95  		data.Set("RedirectUri", *params.RedirectUri)
    96  	}
    97  	if params != nil && params.Audience != nil {
    98  		data.Set("Audience", *params.Audience)
    99  	}
   100  	if params != nil && params.RefreshToken != nil {
   101  		data.Set("RefreshToken", *params.RefreshToken)
   102  	}
   103  	if params != nil && params.Scope != nil {
   104  		data.Set("Scope", *params.Scope)
   105  	}
   106  
   107  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	defer resp.Body.Close()
   113  
   114  	ps := &OauthV1Token{}
   115  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	return ps, err
   120  }