Note that there are some explanatory texts on larger screens.

plurals
  1. POOAuth 2.0 access token isn't refreshed, gives 401 Error : Token Invalid when used
    primarykey
    data
    text
    <p>I am currently using the Youtube API for a desktop application in C++. I am trying to implement the direct upload, which requires an authentication. I naturally choose OAuth 2.0, I followed the <a href="https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Installed_Applications_Flow" rel="nofollow">Google example</a> and apparently everything worked well, I've got an access token and a refresh token, without any error returned.</p> <p>However, when I try to use the access token to upload a video (I put it in the <code>Authorization : Bearer</code> header), I get an error <code>401 : Unauthorized</code> with the description <code>Token Invalid</code>.</p> <p>I then tried to refresh the access token right before requesting the upload (which means I try to refresh right after retrieving the access token, since the two operations are consecutive in my application's flow). The access token remained unchanged : <strong>I received the same access token from the refresh request and the exchange request</strong>.</p> <p>I first thought it meant an access token should be refreshed only when it expires, but it is apparently not true : using the <a href="https://developers.google.com/oauthplayground/" rel="nofollow">OAuth 2.0 Playground</a>, it seems clear that refreshing a not yet expired token works fine and gives a different access token.</p> <p>Any idea on what the problem could be ? Is the 401 error linked to the fact that I am not able to refresh the token ?</p> <p>Edit : Here are the request and the response as shown in Fiddler</p> <p>Request :</p> <pre><code>POST http://uploads.gdata.youtube.com/feeds/api/users/default/uploads HTTP/1.1 Accept: */* Accept-Language: xx Authorization: Bearer MY_ACCESS_TOKEN GData-Version: 2 X-GData-Key: key=MY_DEV_KEY Slug: test.avi Content-Type: multipart/related; boundary="f93dcbA3" Pragma: no-cache User-Agent: SOME_STUFF Host: uploads.gdata.youtube.com Content-Length: 23686 Connection: Keep-Alive --f93dcbA3 Content-Type: application/atom+xml; charset=UTF-8 &lt;?xml version="1.0"?&gt;&lt;entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"&gt;&lt;media:group&gt;&lt;media:title type="plain"&gt;Bad Wedding Toast&lt;/media:title&gt;&lt;media:description type="plain"&gt;I gave a bad toast at my friend's wedding.&lt;/media:description&gt;&lt;media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat"&gt;People&lt;/media:category&gt;&lt;media:keywords&gt;toast, wedding&lt;/media:keywords&gt;&lt;/media:group&gt;&lt;/entry&gt; --f93dcbA3 Content-Type: video/avi Content-Transfer-Encoding: binary &lt;My file binary data&gt; --f93dcbA3-- </code></pre> <p>Response</p> <pre><code>HTTP/1.1 401 Unauthorized X-GData-User-Country: FR WWW-Authenticate: Bearer realm="https://accounts.google.com/o/oauth2/auth",service="youtube" Content-Type: text/html; charset=UTF-8 Content-Length: 13 X-GUploader-UploadID: AEnB2UrVDA94Fk5VFn1ng-2q9VFOo2KifLvIEHFOxQ4m66IUSC8sRf3mo5S8UH94mLyupbfANeLQvxMPhPLo6L0wlcaguQW9CQ Date: Wed, 17 Jul 2013 09:51:23 GMT Server: HTTP Upload Server Built on Jul 8 2013 15:32:26 (1373322746) Token invalid </code></pre> <p>Edit 2 : Request and response using Youtube API v3</p> <p>Request :</p> <pre><code>POST /upload/youtube/v3/videos?part="snippet" HTTP/1.1 Host: www.googleapis.com X-gdata-key: DEV_KEY Content-length: 42190 Content-type: multipart/related; boundary="===============1679429526==" Authorization: ACCESS_TOKEN --===============1679429526== Content-type: application/json { "snippet": { "title": "test" } } --===============1679429526== Content-type: video/avi &lt;BINARY DATA - 41984B&gt; --===============1679429526==-- </code></pre> <p>Response :</p> <pre><code>HTTP/1.1 400 Bad Request Content-length: 229 Via: HTTP/1.1 GWA X-google-cache-control: remote-fetch Server: HTTP Upload Server Built on Jul 8 2013 15:32:26 (1373322746) Date: Wed, 17 Jul 2013 22:14:03 GMT Content-type: application/json { "error": { "errors": [ { "domain": "global", "reason": "badContent", "message": "Unsupported content with type: video/avi" } ], "code": 400, "message": "Unsupported content with type: video/avi" } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COAccess tokens can be refreshed at any time. As a quick check, do you have a Python interpreter on your system? If so a good sanity check is to use the tokens provided by this sample: https://developers.google.com/youtube/v3/code_samples/python. The tokens will be in a .json file - you can also put your C++ tokens into the .json and see what happens. You'll need to install the google-api-python-client via pip/easy_install.
      singulars
    2. COAlso another note: you can plug in your client ID and client secret into the OAuth playground under the "Gear" icon. This'll help you get a usable access token that you can plug into your C++ code.
      singulars
    3. COI seems to be impossible to edit the redirect URIs for an Installed Application, which makes the use of my own credentials in the OAuth 2.0 Playground impossible as well. I created a ClientID for a web application, I could get the tokens in the Playground, tried to use it in my app, still the same error. Doing the request from the Playground itself doesn't return a 401 error, but a "400 Bad request : No file found in request" error, although I suppose this is [a different problem](http://stackoverflow.com/questions/17664700/no-file-found-in-request-with-youtube-api-in-oauth-2-0-playground).
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload