2012-02-17 17 views
5

मैं एक डेमो एप्लिकेशन के लिए फ़्लिकर एपीआई को प्रमाणित करने का प्रयास कर रहा हूं जिसे मैं अपने लिए बनाना चाहता हूं। फिर मैं इस ऐप को नई सुविधाओं के साथ बढ़ा दूंगा जो मैं फ्लिक एपीआई के बारे में सीखूंगा।फ़्लिकर एपीआई (कंसोल में सिमुलेशन) पर हस्ताक्षर अनुरोधों के लिए अमान्य हस्ताक्षर

तो यह सिर्फ कुछ है जिसे मैं खेलना चाहता हूं। लेकिन अब मुझे अनुरोध टोकन प्राप्त करने में कुछ परेशानी है।

मैं यहाँ फ़्लिकर प्रमाणीकरण दस्तावेज़ अनुसरण कर रहा हूं: Flickr Authentication
और मैं यह भी पाया इस Mathlabscript: Flickr API with OAuth-based user authentication

तो इन स्रोतों के आधार पर मेरे पास है अब निम्नलिखित सांत्वना आवेदन:

class Program 
{ 
    private static string Secret = "2b2b2b2b2b2b2b2b2b"; 
    private static string ConsumerKey = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a"; 

    static void Main(string[] args) 
    { 
     Random rand = new Random(); 

     string nonce = rand.Next(9999999).ToString(); 
     string timestamp = ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString(); 

     Console.WriteLine("Nonce: " + nonce); 
     Console.WriteLine("TimeStamp: " + timestamp); 

     Console.WriteLine("ConsumerKey: " + ConsumerKey); 
     Console.WriteLine("AppSecret: " + Secret); 

     //request url 
     StringBuilder b = new StringBuilder(); 
     b.Append("http://www.flickr.com/services/oauth/request_token"); 
     b.Append("?"); 
     b.Append("oauth_nonce="); 
     b.Append(nonce); 
     b.Append("&oauth_timestamp="); 
     b.Append(timestamp); 
     b.Append("&oauth_consumer_key="); 
     b.Append(ConsumerKey); 
     b.Append("&oauth_callback=oob"); 
     b.Append("&oauth_signature_method=HMAC-SHA1"); 

     string requesturl = b.ToString(); 
     Console.WriteLine("RequestUrl: " + requesturl); 

     //base url 
     string basestring; 
     StringBuilder bs = new StringBuilder(); 

     bs.Append("GET&"); 
     bs.Append(UrlHelper.Encode("http://www.flickr.com/services/oauth/request_token")+"&"); 
     basestring = bs.ToString(); 

     StringBuilder p = new StringBuilder(); 
     p.Append("oauth_callback=oob"); 
     p.Append("&oauth_consumer_key="); 
     p.Append(ConsumerKey); 
     p.Append("oauth_nonce="); 
     p.Append(nonce); 
     p.Append("&oauth_signature_method=HMAC-SHA1"); 
     p.Append("&oauth_timestamp="); 
     p.Append(timestamp); 

     string paramers = UrlHelper.Encode(p.ToString()); 

     basestring += paramers; 
     Console.WriteLine("Basestring: " + basestring); 



     System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 

     string key = Secret + "&"; 
     Console.WriteLine("Key: " + key); 

     byte[] keyByte = encoding.GetBytes(key); 

     //--create message to encrypt 
     byte[] messageBytes = encoding.GetBytes(basestring); 

     //--encrypt message using hmac-sha1 with the provided key 
     HMACSHA1 hmacsha1 = new HMACSHA1(keyByte); 
     byte[] hashmessage = hmacsha1.ComputeHash(messageBytes); 

     //--signature 
     string signature = ByteToString(hashmessage); 
     Console.WriteLine("Signature: " + signature); 

     Console.WriteLine("Final Request: " + requesturl + "&oauth_signature=" + signature); 


     Console.ReadKey(true); 



    } 
    public static string ByteToString(byte[] buff) 
    { 
     string sbinary = ""; 

     for (int i = 0; i < buff.Length; i++) 
     { 
      sbinary += buff[i].ToString("X2"); // hex format 
     } 
     return (sbinary); 
    } 
} 

जब मैं यूआरएल को ब्राउज़ करता हूं, यह एप्लिकेशन मुझे देता है, मुझे निम्न प्रतिक्रिया मिलती है:

oauth_problem=signature_invalid&debug_sbs=GET&http%3A%2F%2Fwww.flickr.com%2Fservices%2Foauth%2Frequest_token&oauth_callback%3Dhttp%253A%252F%252Fwww.google.be%26oauth_consumer_key%3D1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a%26oauth_nonce%3D27504343%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1329469580 

ऐसा लगता है कि अनुरोध के लिए मेरा हस्ताक्षर अमान्य है।

मुझे आशा है कि कोई मुझे इन अनुरोधों के लिए सही हस्ताक्षर प्राप्त करने में मदद कर सकता है।

मुझे पता है कि एक फ़्लिकनेट लाइब्रेरी है जो पहले से ही अधिकांश डेवलपर्स के लिए कड़ी मेहनत कर चुकी है लेकिन मुझे लगता है कि यह काम करने के लिए भी उपयोगी हो सकता है। मैंने फ़्लिकरनेट के स्रोत कोड में देखा लेकिन इस कोड को पूरा करने के लिए अंतिम शांति नहीं मिली।

मुझे बताएं कि क्या आप मेरी मदद कर सकते हैं। यह बहुत अच्छा होगा!

धन्यवाद!

+0

अपने आप को ध्यान दें: ओथ प्रमाणीकरण के बारे में अधिक जानकारी यहां मिल सकती है: http://hueniverse.com/oauth/guide/authentication/ जितनी जल्दी हो सके मैं इसे देख लूंगा :) – ThdK

उत्तर

9

ठीक है, मुझे अंत में जवाब मिल गया। ओथ के लिए अनुरोधों पर हस्ताक्षर करते समय आपको कुछ बातें ध्यान में रखना है।

  1. हस्ताक्षर HMAC-SHA1 basestring का उपयोग कर (nr.2 देखें) के रूप में पाठ एन्क्रिप्ट होने के लिए और clientsecret और token_secret (यदि आपके पास है) के साथ एन्क्रिप्टेड किया जाना चाहिए (एन.आर. देखते हैं। 3)
  2. basestring = [HttpMethod] & [FlickrAPIEndpoint] & [पैरामीटर] अनुरोध oauth_token के लिए
  3. कुंजी = [ApiSecret] & (या कुंजी = [ApiSecret] & [oauth_token_secret] अनुरोध ACCESS_TOKEN के लिए)

महत्वपूर्ण: FlickrAPIEndPoint और पैरामीटर्स को UrlEncoded होना चाहिए (दो भागों में!) मैंने एन्कोडिंग के लिए एक अलग वर्ग का उपयोग किया है क्योंकि HttpUtility.UrlEncode विधि लोअरकेस एन्कोडिंग का उपयोग करती है जबकि अपरकेस एन्कोडिंग का उपयोग किया जाना चाहिए।

महत्वपूर्ण: पैरामीटर वर्णमाला क्रम में होना चाहिए!

यहां एक कंसोल एप्लिकेशन के लिए कोड है जो एक अनुरोध टोकन और अनुरोध टोकन गुप्त के लिए एक हस्ताक्षरित अनुरोध तैयार करेगा।इयान हॉपकिंस द्वारा

class Program 
{ 
    private static string Secret = "9dcc18a121e9a02e"; 
    private static string ConsumerKey = "3aafc63ec6b05f3f9a9ff3a1c35ce541"; 
    private static string request_token = ""; 

    static void Main(string[] args) 
    { 

     string requestString = "http://www.flickr.com/services/oauth/request_token"; 

     //generate a random nonce and a timestamp 
     Random rand = new Random(); 
     string nonce = rand.Next(999999).ToString(); 
     string timestamp = GetTimestamp(); 

     //create the parameter string in alphabetical order 
     string parameters = "oauth_callback=" + UrlHelper.Encode("http://www.example.com"); 
     parameters += "&oauth_consumer_key=" + ConsumerKey; 
     parameters += "&oauth_nonce=" + nonce; 
     parameters += "&oauth_signature_method=HMAC-SHA1"; 
     parameters += "&oauth_timestamp=" + timestamp; 
     parameters += "&oauth_version=1.0"; 

     //generate a signature base on the current requeststring and parameters 
     string signature = generateSignature("GET", requestString, parameters); 

     //add the parameters and signature to the requeststring 
     string url = requestString + "?" + parameters + "&oauth_signature=" + signature; 

     //test the request 
     WebClient web = new WebClient(); 
     string result = web.DownloadString(url); 

     Console.WriteLine("Flickr Response: "); 
     Console.WriteLine(result); //contains the oauth_token and the oauth_token_secret 
     Console.ReadKey(true); 

    } 

    private static string generateSignature(string httpMethod, string ApiEndpoint, string parameters) 
    { 
     //url encode the API endpoint and the parameters 

     //IMPORTANT NOTE: 
     //encoded text should contain uppercase characters: '=' => %3D !!! (not %3d) 
     //the HtmlUtility.UrlEncode creates lowercase encoded tags! 
     //Here I use a urlencode class by Ian Hopkins 
     string encodedUrl = UrlHelper.Encode(ApiEndpoint); 
     string encodedParameters = UrlHelper.Encode(parameters); 

     //generate the basestring 
     string basestring = httpMethod + "&" + encodedUrl + "&"; 
     parameters = UrlHelper.Encode(parameters); 
     basestring = basestring + parameters; 

     //hmac-sha1 encryption: 

     System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 

     //create key (request_token can be an empty string) 
     string key = Secret + "&" + request_token; 
     byte[] keyByte = encoding.GetBytes(key); 

     //create message to encrypt 
     byte[] messageBytes = encoding.GetBytes(basestring); 

     //encrypt message using hmac-sha1 with the provided key 
     HMACSHA1 hmacsha1 = new HMACSHA1(keyByte); 
     byte[] hashmessage = hmacsha1.ComputeHash(messageBytes); 

     //signature is the base64 format for the genarated hmac-sha1 hash 
     string signature = System.Convert.ToBase64String(hashmessage); 

     //encode the signature to make it url safe and return the encoded url 
     return UrlHelper.Encode(signature); 

    } 

    //generator of unix epoch time 
    public static String GetTimestamp() 
    { 
     int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; 
     return epoch.ToString(); 
    } 

} 

UrlHelper वर्ग यूआरएल एन्कोडिंग

/// <summary> 
    /// URL encoding class. Note: use at your own risk. 
    /// Written by: Ian Hopkins (http://www.lucidhelix.com) 
    /// Date: 2008-Dec-23 
    /// (Ported to C# by t3rse (http://www.t3rse.com)) 
    /// </summary> 
    public class UrlHelper 
    { 
     public static string Encode(string str) 
     { 
      var charClass = String.Format("0-9a-zA-Z{0}", Regex.Escape("-_.!~*'()")); 
      return Regex.Replace(str, 
       String.Format("[^{0}]", charClass), 
       new MatchEvaluator(EncodeEvaluator)); 
     } 
    public static string EncodeEvaluator(Match match) 
    { 
     return (match.Value == " ") ? "+" : String.Format("%{0:X2}", Convert.ToInt32(match.Value[0])); 
    } 

    public static string DecodeEvaluator(Match match) 
    { 
     return Convert.ToChar(int.Parse(match.Value.Substring(1), System.Globalization.NumberStyles.HexNumber)).ToString(); 
    } 

    public static string Decode(string str) 
    { 
     return Regex.Replace(str.Replace('+', ' '), "%[0-9a-zA-Z][0-9a-zA-Z]", new MatchEvaluator(DecodeEvaluator)); 
    } 
} 
+0

बहुत मदद करता है, धन्यवाद ! –

1

क्या आप इसे स्क्रैच से लिख रहे हैं? यदि हां, तो आपको नहीं करना चाहिए। इसके बजाय http://flickrnet.codeplex.com/ का उपयोग करें। इस पुस्तकालय ने आपके लिए भारी भारोत्तोलन कर लिया है।

+1

मैं flickrnet पुस्तकालय डाउनलोड किया है के लिए इस्तेमाल किया, लेकिन मैं समझने के लिए यह क्या करता है चाहता था। क्योंकि एक बहुत ही छोटे ऐप के लिए, जहां मुझे केवल फ़्लिकर एपीआई को केवल एक या कुछ अनुरोध करने की आवश्यकता है, मैंने सोचा कि मैं इसे स्वयं लिख सकता हूं। (मैंने पिछले साल फेसबुक एपीआई के लिए भी ऐसा किया था) – ThdK

+1

@ थॉमसडेकीर कोड कोड कोडप्लेक्स पर है, ताकि आप इसे डाउनलोड करके और स्रोत ब्राउज़ करके यह देख सकें। – Jeff

+0

ठीक है ऐसा लगता है कि मुझे पहले कुछ और शोध की आवश्यकता थी। मैंने आवश्यक जानकारी ढूंढने के बिना फ़्लिकरनेट के स्रोत कोड में खोज की। अब, सभी प्रकार की प्रोग्रामिंग भाषाओं में कई उदाहरण पढ़ने के बाद, मैंने फ़्लिकर एपीआई के अनुरोध के निर्माण को अनुकरण करने वाला एक कंसोल एप्लिकेशन बनाया है। लेकिन मुझे अमान्य हस्ताक्षर संदेश मिलता है। मैंने यहां सभी जानकारी और कोड के साथ एक नया प्रश्न बनाया है: http://stackoverflow.com/questions/9330004/invalid-signature-for-igning-requests-to-the-flickr-api-simulation-in -कॉन्सोल – ThdK

संबंधित मुद्दे