# Golang sdk and oauth2 (authorization code)

**URL:** <https://devforum.okta.com/t/golang-sdk-and-oauth2-authorization-code/13311>\
**Category:** OAuth/OIDC\
**Created:** [March 11, 2021, 8:27pm UTC](https://devforum.okta.com/t/golang-sdk-and-oauth2-authorization-code/13311 "2021-03-11T20:27:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kungfoo](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/kungfoo/32/2719_2.png) [@kungfoo](https://devforum.okta.com/u/kungfoo)\
**Post date:** [March 11, 2021, 8:27pm UTC](https://devforum.okta.com/t/golang-sdk-and-oauth2-authorization-code/13311/1 "2021-03-11T20:27:24Z")

</div>

Hello, everyone.

I’m trying to create a golang cli to interact with the API. I managed to do this authenticating with an API Token provided by the user, stored in a file or in an envvar.

Now I am trying to add oauth2 access, the goal is:

- when the user runs the script it opens a browser with the okta login and creates an http server behind the scenes to catch the redirect.
- the user logs in
- the server catches the data and extracts the id and access tokens and the JSESSIONID
- the server is shutdown
- and the script uses the data to call the sdk

So far I am getting the credentials, but I can’t figure out how to create the new sdk client with them (I mean, create the client with an API token is easy, but how can I pass to it the id and access tokens and the JSESSIONID?)

Using these:

```auto
      "github.com/okta/okta-sdk-golang/v2/okta"
      "github.com/okta/okta-sdk-golang/v2/okta/query"
      "github.com/okta/okta-jwt-verifier-golang"

```

Any suggestions?

Thanks

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [March 15, 2021, 8:23pm UTC](https://devforum.okta.com/t/golang-sdk-and-oauth2-authorization-code/13311/2 "2021-03-15T20:23:22Z")

</div>

The Golang management SDK is not designed to take in an access token directly to authorize requests against Okta endpoints, but you can configure it to use a Service app configured with private\_key\_jwt and it will request tokens for you. Details [here](https://github.com/okta/okta-sdk-golang#oauth-20)

---

<div class="post-metadata">

**Author:** ![kungfoo](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/kungfoo/32/2719_2.png) [@kungfoo](https://devforum.okta.com/u/kungfoo)\
**Post date:** [March 19, 2021, 1:41pm UTC](https://devforum.okta.com/t/golang-sdk-and-oauth2-authorization-code/13311/3 "2021-03-19T13:41:33Z")

</div>

Thanks, Andrea.

I understand. My goal is to distribute the script, but only let authorized users use it. So, if the script is copied by someone else, she/he won’t have authorization.

Afaik, the private\_key\_jwt way will request the tokens using this key and with no intervention from the user side, am I right?

I mean, if I use what I have now, but hitting directly the API instead of using the sdk, I believe I can use the data I fetched. Maybe the solution would be to get rid of the sdk and use the raw api call?

---

<div class="post-metadata">

**Author:** ![andrea](https://sea1.discourse-cdn.com/oktadev/user_avatar/devforum.okta.com/andrea/32/2172_2.png) [@andrea](https://devforum.okta.com/u/andrea)\
**Post date:** [February 6, 2024, 11:21pm UTC](https://devforum.okta.com/t/golang-sdk-and-oauth2-authorization-code/13311/4 "2024-02-06T23:21:33Z")

</div>

Closed on February 6, 2024, 11:21pm UTC


