question

austiwend_4 avatar image
0 Likes"
austiwend_4 asked ·

Can't get authToken using OAuth2 C#

Any help you could give me would be great. I've been banging my head into a wall for quite a while. I am trying to set up API calls to the Account API, and need to generate OAuth user tokens before I can do so. I created a new developer account and have generated keysets for a sandbox environment. Using those, I am trying to get a token... here's the code: var authCode = "myCode"; //var encodedAuth = HttpUtility.UrlEncode(authCode); var clientID = "myAppID"; var secret = "mySecret"; var ruName = "myRedirect"; var temp = clientID + ":" + secret; var bytes = System.Text.Encoding.UTF8.GetBytes(temp); var auth = System.Convert.ToBase64String(bytes); using (var client = new HttpClient()) { client.BaseAddress = new Uri(" https://api.sandbox.ebay.com/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("Authorization", "Basic " + auth); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "identity/v1/oauth2/token"); request.Content = new FormUrlEncodedContent(new[] { new KeyValuePair ("grant_type", "authorization_code"), new KeyValuePair ("code", authCode), new KeyValuePair ("redirect_uri", ruName) }); var result = client.SendAsync(request).Result; var response = result.Content.ReadAsStringAsync().Result; } This returns an error saying that the authorization code is either invalid or was granted to another user. I am getting the authorization code (authCode, first line) as the image shows: ![alt text][1] [1]: /storage/temp/1528-oauth.png This doesn't work. I figured maybe the button is actually giving me back a real OAuth token, so I tried inserting it into the call I've been trying to make, like so: var endpoint = "sell/account/v1/program/get_opted_in_programs"; using (var client = new HttpClient()) { client.BaseAddress = new Uri(EbayBaseEndpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("X-EBAY-C-MARKETPLACE-ID", "EBAY-US"); client.DefaultRequestHeaders.Add("Authorization", "TOKEN " + "myToken"); HttpResponseMessage result = client.GetAsync(endpoint).Result; var response = result.Content.ReadAsStringAsync().Result; } This fails too, with a mix of Unauthorized and Bad Request errors. What am I doing wrong here?
oauth2authentication failed
oauth.png (36.8 KiB)
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

austiwend_4 avatar image
1 Like"
austiwend_4 answered ·
I am using the "generate token" button because when following the URL like the documentation suggests to: ( https://signin.sandbox.ebay.com/authorize? client_id= & redirect_uri= & response_type=code& state= & scope=https%3A%2F% 2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.account%20 https%3A%2F% 2Fapi.ebay.com%2Foauth%2Fapi_scope%2Fsell.inventory) I get taken to an error page that says "Sorry, we are unable to complete your request. Please try again later. If I look at the URL I hit, I get this: https://signin.sandbox.ebay.com/ws/eBayISAPI.dll?OAuthAuthorize&error=CLIENT_ID_NOT_EXISTS?error=invalid_client. It's as if my client ID doesn't eixst, but I am pulling it straight from here... sorry for the terrible paint job. ![alt text][1] [1]: /storage/temp/1529-token.png

token.png (86.0 KiB)
·
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

avishek_developersprogram avatar image
0 Likes"
avishek_developersprogram answered ·
Hi austiwend_4, Thank you for reporting this. Can you please provide us your developer account username so we can investigate this from our end? Thank you, avishek_developersprogram
·
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

fmvallim avatar image
0 Likes"
fmvallim answered ·
Hey guys, I'm getting the same error while trying to get the authorization code.![alt text][1] [1]: /storage/temp/1578-ebay-client-id-not-exists.png Do you have any news about this?

·
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

srikrish1 avatar image
0 Likes"
srikrish1 answered ·
Hi fabricivalli-0, Please refer to this guide to get an oauth user token. https://developer.ebay.com/api-docs/static/oauth-gen-user-token.html If you are still having issues, please let us know your developer user name and we take a look. Thanks, Krishna Rajagopal eBay Developers Program.
·
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

fmvallim avatar image
0 Likes"
fmvallim answered ·
Hi srikrish1, I'm following the guide that you commented about, even so still I'm having issues. My username is: **fvallim** Please, could you take a look if has some issue? Thanks!
·
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

krich_developersupport avatar image
0 Likes"
krich_developersupport answered ·
The *authorization code* is returned from a permission grant, which you get from a URL redirect request. This is described in the following doc: https://developer.ebay.com/api-docs/static/oauth-permissions-grant.html Note that you do not get an *authorization code* from the Developer Program UI when you grant permissions through the User token flow. The UI automatically converts the authorization code into an access token, and the UI returns the access token--the authorization code is hidden in this processing. To use the access token in a request, use the following C# statement (change the word 'TOKEN' to "Bearer'): ` client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "myToken");`
·
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.