Generate a new access token with refresh token(c#)

3,791 views
Skip to first unread message

Richard

unread,
Apr 8, 2014, 7:27:15 PM4/8/14
to adwor...@googlegroups.com
Is there an example in C# to generate a new access token from refresh token without using the client library? 
Our currently application are using raw soap call to web service without using client library so we would like to keep it this way. Thanks!

Josh Radcliff (AdWords API Team)

unread,
Apr 9, 2014, 10:45:04 AM4/9/14
to adwor...@googlegroups.com
Hi,

The OAuth2ProviderForApplications class may be a good starting point. You could also look at the OAuth2 documentation, which explains the flow and formats of the requests and responses involved.

Cheers,
Josh, AdWords API Team

Richard

unread,
Apr 17, 2014, 1:22:38 PM4/17/14
to adwor...@googlegroups.com
Hi,
Thanks for the link, it's very helpful. 
So I wrote another post method to get a renew token, however I am getting error "(400) Bad Request.".
is it because the application I defined as a "web application" (I got my first accesstoken from the call back link) not "Installed Application" ? 



        public GoogleAccessTokenData GetAccessTokenDataFromRefreshToken(string authorizationCode, string refreshToken)
        {
            string Url = "https://accounts.google.com/o/oauth2/token";
            string data = "code={0}&client_id={1}&client_secret={2}&refresh_token={3}&grant_type=refresh_token";

            HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
            string result = null;
            request.Method = "POST";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            string param = string.Format(data, authorizationCode, _GoogleClientId, _GoogleSecret, refreshToken);
            var bs = Encoding.UTF8.GetBytes(param);
            using (Stream reqStream = request.GetRequestStream())  <=== exception thrown with error "(400) Bad Request"
            {
                reqStream.Write(bs, 0, bs.Length);
            }

            using (WebResponse response = request.GetResponse())
            {
                var sr = new StreamReader(response.GetResponseStream());
                result = sr.ReadToEnd();
                sr.Close();
            }
            var jsonSerializer = new JavaScriptSerializer();
            var tokenData = jsonSerializer.Deserialize<GoogleAccessTokenData>(result);
            return tokenData;
        }

Richard

unread,
Apr 17, 2014, 3:19:53 PM4/17/14
to adwor...@googlegroups.com
Ok I found the problem, I don't need to pass authorization code again to get the renewed access token. Thanks.
Reply all
Reply to author
Forward
0 new messages