Click here to Skip to main content
15,902,299 members
Home / Discussions / C#
   

C#

 
QuestionC# ComboBox (DataBound) for display Pin
maxpfc29-Jun-10 6:50
maxpfc29-Jun-10 6:50 
AnswerRe: C# ComboBox (DataBound) for display Pin
Ennis Ray Lynch, Jr.29-Jun-10 7:40
Ennis Ray Lynch, Jr.29-Jun-10 7:40 
GeneralRe: C# ComboBox (DataBound) for display Pin
DaveyM6929-Jun-10 8:02
professionalDaveyM6929-Jun-10 8:02 
GeneralRe: C# ComboBox (DataBound) for display Pin
I Believe In GOD29-Jun-10 8:16
I Believe In GOD29-Jun-10 8:16 
JokeRe: C# ComboBox (DataBound) for display Pin
Pete O'Hanlon29-Jun-10 11:07
mvePete O'Hanlon29-Jun-10 11:07 
GeneralRe: C# ComboBox (DataBound) for display Pin
DaveyM6929-Jun-10 12:04
professionalDaveyM6929-Jun-10 12:04 
AnswerRe: C# ComboBox (DataBound) for display Pin
maxpfc29-Jun-10 23:30
maxpfc29-Jun-10 23:30 
QuestionOAuth implementation help Pin
Etienne_12329-Jun-10 5:44
Etienne_12329-Jun-10 5:44 
Hi
I'm trying to implement OAuth using Google's implementation. First of all I create a few of the parameters using methods from OAuthBase.cs as follows:

string consumerKey = "anonymous"; //key
string consumerSecret = "anonymous";
Uri uri = new Uri("https://www.google.com/accounts/OAuthGetRequestToken");     

OAuthBase oAuth = new OAuthBase();
string nonce = oAuth.GenerateNonce();
string timeStamp = oAuth.GenerateTimeStamp();
string str = "https://www.google.com/accounts/OAuthGetRequestToken";
string str2 = "oauth_consumer_key=www.google.com&oauth_nonce=1932530&oauth_timestamp=1277400332&oauth_signature_method=HMAC-SHA1&oauth_version=1.0";


and the methods in the OAuthBase class:

public virtual string GenerateNonce() 
{
    // Just a simple implementation of a random number between 123400 and 9999999
    return random.Next(123400, 9999999).ToString();            
}

public virtual string GenerateTimeStamp() 
        {
            // Default implementation of UNIX time of the current UTC time
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string timeStamp = ts.TotalSeconds.ToString();
            timeStamp = timeStamp.Substring(0, timeStamp.IndexOf("."));
            return timeStamp;               
        }


Then I create the signature as follows:

string sig = oAuth.GenerateSignature(uri,
consumerKey, consumerSecret,
string.Empty, string.Empty,
"GET", timeStamp, nonce,
OAuthBase.SignatureTypes.HMACSHA1, out str, out str2 );


using this method from OAuthBase.cs:

public string GenerateSignature(Uri url, string consumerKey, string consumerSecret, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, SignatureTypes signatureType, out string normalizedUrl, out string normalizedRequestParameters) 
{
	normalizedUrl = null;
	normalizedRequestParameters = null;

        switch (signatureType) 
        {
                case SignatureTypes.PLAINTEXT:					
                    return HttpUtility.UrlEncode(string.Format("{0}&{1}", consumerSecret, tokenSecret));
                case SignatureTypes.HMACSHA1:					
		    string signatureBase = GenerateSignatureBase(url, consumerKey, token, tokenSecret, httpMethod, timeStamp, nonce, HMACSHA1SignatureType, out normalizedUrl, out normalizedRequestParameters);

                    HMACSHA1 hmacsha1 = new HMACSHA1();
                    hmacsha1.Key = Encoding.ASCII.GetBytes(string.Format("{0}&{1}", UrlEncode(consumerSecret), string.IsNullOrEmpty(tokenSecret) ? "" : UrlEncode(tokenSecret)));

                    return GenerateSignatureUsingHash(signatureBase, hmacsha1);                                        
                case SignatureTypes.RSASHA1:
                    throw new NotImplementedException();
                default:
                    throw new ArgumentException("Unknown signature type", "signatureType");
            }
        }


Here is the GenerateSignatureBase method that gets called if the signature type equals HMACSHA1:

public string GenerateSignatureBase(Uri url, string consumerKey, string token, string tokenSecret, string httpMethod, string timeStamp, string nonce, string signatureType, out string normalizedUrl, out string normalizedRequestParameters) {
            if (token == null) {
                token = string.Empty;
            }

            if (tokenSecret == null) {
                tokenSecret = string.Empty;
            }

            if (string.IsNullOrEmpty(consumerKey)) {
                throw new ArgumentNullException("consumerKey");
            }

            if (string.IsNullOrEmpty(httpMethod)) {
                throw new ArgumentNullException("httpMethod");
            }

            if (string.IsNullOrEmpty(signatureType)) {
                throw new ArgumentNullException("signatureType");
            }

			normalizedUrl = null;
			normalizedRequestParameters = null;

            List<QueryParameter> parameters = GetQueryParameters(url.Query);
            parameters.Add(new QueryParameter(OAuthVersionKey, OAuthVersion));
            parameters.Add(new QueryParameter(OAuthNonceKey, nonce));
            parameters.Add(new QueryParameter(OAuthTimestampKey, timeStamp));
            parameters.Add(new QueryParameter(OAuthSignatureMethodKey, signatureType));
            parameters.Add(new QueryParameter(OAuthConsumerKeyKey, consumerKey));

            if (!string.IsNullOrEmpty(token)) {
                parameters.Add(new QueryParameter(OAuthTokenKey, token));
            }

            parameters.Sort(new QueryParameterComparer());

            normalizedUrl = string.Format("{0}://{1}", url.Scheme, url.Host);
            if (!((url.Scheme == "http" && url.Port == 80) || (url.Scheme == "https" && url.Port == 443)))
            {
                normalizedUrl += ":" + url.Port;
            }
            normalizedUrl += url.AbsolutePath;
            normalizedRequestParameters = NormalizeRequestParameters(parameters);

            StringBuilder signatureBase = new StringBuilder();			
            signatureBase.AppendFormat("{0}&", httpMethod.ToUpper());
            signatureBase.AppendFormat("{0}&", UrlEncode(normalizedUrl));
            signatureBase.AppendFormat("{0}", UrlEncode(normalizedRequestParameters));

            return signatureBase.ToString();
        }


I then use the string builder to build the string that I will pass to the WebRequest as follows:

sig = HttpUtility.UrlEncode(sig);

StringBuilder sb = new StringBuilder(uri.ToString());
sb.AppendFormat("?oauth_consumer_key={0}&", consumerKey);
sb.AppendFormat("oauth_nonce={0}&", nonce);
sb.AppendFormat("oauth_timestamp={0}&", timeStamp);
sb.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
sb.AppendFormat("oauth_version={0}&", "1.0");

sb.AppendFormat("scope={0}&", "https://mail.google.com/mail/feed/atom");

sb.AppendFormat("oauth_signature={0}", sig);



I then create a WebRequest as follows and pass it the string:

string s = sb.ToString();
WebRequest w = WebRequest.Create(s);


..where 'string s' equals:

"https://www.google.com/accounts/OAuthGetRequestToken?oauth_consumer_key=anonymous&oauth_nonce=1221378&oauth_timestamp=1277825766&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&scope=https://mail.google.com/mail/feed/atom&oauth_signature=n5HsVCqhujRoMzBKbRXa%2fC6hk2M%3d"


As soon as I call:

Stream objStream = w.GetResponse().GetResponseStream();


I get the following error: The remote server returned an error: (400) Bad Request.


So please could anyone tell me whether you can see what's wrong with my code or the string that I pass in.
AnswerRe: OAuth implementation help Pin
Ennis Ray Lynch, Jr.29-Jun-10 6:49
Ennis Ray Lynch, Jr.29-Jun-10 6:49 
GeneralRe: OAuth implementation help Pin
Etienne_12329-Jun-10 7:32
Etienne_12329-Jun-10 7:32 
AnswerRe: OAuth implementation help [modified] Pin
Luc Pattyn29-Jun-10 7:07
sitebuilderLuc Pattyn29-Jun-10 7:07 
GeneralRe: OAuth implementation help Pin
Etienne_12329-Jun-10 7:50
Etienne_12329-Jun-10 7:50 
GeneralRe: OAuth implementation help Pin
Luc Pattyn29-Jun-10 8:06
sitebuilderLuc Pattyn29-Jun-10 8:06 
GeneralRe: OAuth implementation help Pin
Etienne_12329-Jun-10 23:29
Etienne_12329-Jun-10 23:29 
GeneralRe: OAuth implementation help Pin
Luc Pattyn30-Jun-10 0:34
sitebuilderLuc Pattyn30-Jun-10 0:34 
GeneralRe: OAuth implementation help Pin
Etienne_12330-Jun-10 2:00
Etienne_12330-Jun-10 2:00 
GeneralRe: OAuth implementation help Pin
Ennis Ray Lynch, Jr.30-Jun-10 3:00
Ennis Ray Lynch, Jr.30-Jun-10 3:00 
QuestionHow to remove blank pages in a PDF from C# Application [modified] Pin
kalyan_vb29-Jun-10 5:31
kalyan_vb29-Jun-10 5:31 
Questionclass concept Pin
Yonathan111129-Jun-10 1:46
professionalYonathan111129-Jun-10 1:46 
AnswerRe: class concept Pin
J4amieC29-Jun-10 2:23
J4amieC29-Jun-10 2:23 
GeneralRe: class concept Pin
Yonathan111130-Jun-10 20:37
professionalYonathan111130-Jun-10 20:37 
AnswerRe: class concept Pin
Richard MacCutchan29-Jun-10 2:42
mveRichard MacCutchan29-Jun-10 2:42 
GeneralRe: class concept Pin
Yonathan111130-Jun-10 20:39
professionalYonathan111130-Jun-10 20:39 
AnswerRe: class concept Pin
PIEBALDconsult29-Jun-10 3:47
mvePIEBALDconsult29-Jun-10 3:47 
GeneralRe: class concept Pin
Yonathan111130-Jun-10 20:43
professionalYonathan111130-Jun-10 20:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.