The code fails to "Upgrade to Access Token" but fully matches the AOuth Playground requests.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Net;
namespace TestGAOuth
{
class Program
{
const string OAuthGetRequestToken = "https://www.google.com/accounts/OAuthGetRequestToken";
const string OAuthAuthorizeToken = "https://www.google.com/accounts/OAuthAuthorizeToken";
const string OAuthGetAccessToken = "https://www.google.com/accounts/OAuthGetAccessToken";
static void Main(string[] args)
{
//1) Choose your scope:
string scope = "https://www.google.com/analytics/feeds/";
//2)Modify the OAuth Parameters:
string oauth_consumer_key = "anonymous";
string consumerSecret = "anonymous";
//3)Get a Request Token:
string timestamp = GenerateTimeStamp();
string nonce = GenerateNonce();
//Signature:
StringBuilder sb = new StringBuilder();
sb.AppendFormat("oauth_callback={0}", "oob");
sb.AppendFormat("&oauth_consumer_key={0}", oauth_consumer_key);
sb.AppendFormat("&oauth_nonce={0}", nonce);
sb.Append("&oauth_signature_method=HMAC-SHA1");
sb.AppendFormat("&oauth_timestamp={0}", timestamp);
sb.Append("&oauth_version=1.0");
sb.AppendFormat("&scope={0}", UrlEncode(scope));
StringBuilder signatureBase = new StringBuilder("GET");
signatureBase.AppendFormat("&{0}", UrlEncode(OAuthGetRequestToken));
signatureBase.AppendFormat("&{0}", UrlEncode(sb.ToString()));
string sign = signatureBase.ToString();
Debug.WriteLine("Signature Base 1:" + sign);
string key = string.Format("{0}&", UrlEncode(consumerSecret));
HMACSHA1 hmacsha1 = new HMACSHA1();
hmacsha1.Key = Encoding.UTF8.GetBytes(key);
byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(sign);
byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
string signature = Convert.ToBase64String(hashBytes);
string header1 = string.Format("OAuth oauth_consumer_key=\"{0}\",oauth_callback=\"oob\",oauth_nonce=\"{1}\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"{2}\",oauth_version=\"1.0\",oauth_signature=\"{3}\"",
UrlEncode(oauth_consumer_key), UrlEncode(nonce), UrlEncode(timestamp), UrlEncode(signature));
string request1 = OAuthGetRequestToken + string.Format("?scope={0}", UrlEncode(scope));
WebClient wc = new WebClient();
wc.Headers["Authorization"] = header1;
string result = wc.DownloadString(request1);
Debug.Write(result);
string[] pairs = result.Split('&');
string oauth_token = pairs[0].Substring(12);
string oauth_token_secret = pairs[1].Substring(19);
//4)Authorize the Request Token:
string request2 = OAuthAuthorizeToken + string.Format("?oauth_token={0}&hd=default", oauth_token);
System.Diagnostics.Process.Start(request2);//launch browser
Console.WriteLine("Please input the pin code:");
string pin = Console.ReadLine();
//5)Upgrade to an Access Token:
timestamp = GenerateTimeStamp();
nonce = GenerateNonce();
string request3 = OAuthGetAccessToken;
StringBuilder sb3 = new StringBuilder();
sb3.AppendFormat("oauth_consumer_key={0}", oauth_consumer_key);
sb3.AppendFormat("&oauth_nonce={0}", nonce);
sb3.Append("&oauth_signature_method=HMAC-SHA1");
sb3.AppendFormat("&oauth_timestamp={0}", timestamp);
sb3.AppendFormat("&oauth_token={0}", UrlEncode(oauth_token));
sb3.AppendFormat("&oauth_verifier={0}", UrlEncode(pin));
sb3.Append("&oauth_version=1.0");
StringBuilder signatureBase3 = new StringBuilder("GET");
signatureBase3.AppendFormat("&{0}", UrlEncode(OAuthGetAccessToken));
signatureBase3.AppendFormat("&{0}", UrlEncode(sb3.ToString()));
string sign3 = signatureBase3.ToString();
Debug.WriteLine("Signature Base 3:" + sign3);
string key3 = string.Format("{0}&{1}", UrlEncode(consumerSecret), UrlEncode(oauth_token_secret));
HMACSHA1 hmacsha3 = new HMACSHA1();
hmacsha3.Key = Encoding.UTF8.GetBytes(key3);
byte[] dataBuffer3 = System.Text.Encoding.UTF8.GetBytes(sign3);
byte[] hashBytes3 = hmacsha3.ComputeHash(dataBuffer3);
string signature3 = Convert.ToBase64String(hashBytes3);
string header3 = string.Format("OAuth oauth_version=\"1.0\", oauth_nonce=\"{0}\", oauth_timestamp=\"{1}\", oauth_consumer_key=\"{2}\", oauth_verifier=\"{3}\", oauth_token=\"{4}\", oauth_signature_method=\"HMAC-SHA1\", oauth_signature=\"{5}\"",
UrlEncode(nonce), UrlEncode(timestamp), UrlEncode(oauth_consumer_key), UrlEncode(pin), UrlEncode(oauth_token), UrlEncode(signature3));
WebClient wc3 = new WebClient();
wc3.Headers["Authorization"] = header3;
string result3 = wc3.DownloadString(request3);
Debug.Write(result3);
}
private static string GenerateTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalSeconds).ToString();
}
private static string GenerateNonce()
{
Random rnd = new Random(Environment.TickCount);
return rnd.Next(123400, 9999999).ToString("X");
}
static string UrlEncode(string value)
{
if (value == null)
return string.Empty;
StringBuilder result = new StringBuilder();
foreach (char symbol in value)
{
if ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~".IndexOf(symbol) != -1)
{
result.Append(symbol);
}
else
{
result.Append('%' + String.Format("{0:X2}", (int)symbol));
}
}
return result.ToString();
}
}
}