2011-06-06 21 views
5

मैं कुछ Google API के साथ संयोजन में OAuth 2.0 के साथ थोड़ा सा खेल रहा हूं। हालांकि प्राधिकरण प्रक्रिया काफी आसान है, प्रारंभिक प्रमाणीकरण पूरा होने के बाद मुझे स्वचालित प्राधिकरण के साथ समस्या का सामना करना पड़ रहा है।Google/OAuth 2 - स्वचालित लॉगऑन

तो:

1. Authorization is done for the first time. (user grants access, I get the token etc etc) 
2. User exits the application 
3. User starts the application again 
4. How to logon automatically here? 

बिंदु 4 में, मैं एक refresh_token की क्या ज़रूरत है तो मैं सिर्फ इतना है कि request_token का उपयोग कर एक नया टोकन का अनुरोध करना चाहिए। लेकिन मुझे अभी भी मेरी कॉल पर 401 अनधिकृत परिणाम मिलते रहेंगे।

तो मैं जो करने का प्रयास करता हूं वह यह है कि मेरा एप्लिकेशन चुपचाप लॉगऑन कर सकता है ताकि उपयोगकर्ता को हर बार पहुंच प्रदान न हो।

उत्तर

2

आप नीचे दिए गए अनुरोध का उपयोग OAuth ताज़ा करने के लिए 2.0 टोकन सक्षम होना चाहिए:

POST /o/oauth2/token HTTP/1.1 
Host: accounts.google.com 
Content-Type: application/x-www-form-urlencoded 

client_id=21302922996.apps.googleusercontent.com& 
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB& 
refresh_token=1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ& 
grant_type=refresh_token 

Google OAuth 2.0 documentation में बताया है।

मैं सिर्फ यह कर्ल का उपयोग कर बाहर की कोशिश की और यह उम्मीद के रूप में काम करता है: Google.GData.Client का उपयोग करके

curl -d client_id=$CLIENT_ID -d client_secret=$CLIENT_SECRET -d refresh_token=$REFRESH_TOKEN -d grant_type=refresh_token https://accounts.google.com/o/oauth2/token 

{"access_token":"$ACCESS_TOKEN","token_type":"Bearer","expires_in":3600} 
+0

मैं अभी भी क्या मैं गलत किया था पता नहीं है ; लेकिन किसी भी तरह से यह अब काम करता है! धन्यवाद – Rhapsody

1

मैं नेट में करते हैं। एक बार जब मैं प्राधिकरण प्रक्रिया और टोकन को सहेजता हूं, तो अगली बार जब मेरा उपयोगकर्ता साइट पर आता है तो मैं एक GOAuthRequestFactory ऑब्जेक्ट उत्पन्न करके प्राधिकरण खींचता हूं।

public GOAuthRequestFactory GetGoogleOAuthFactory(int id) 
    { 
     // build the base parameters 
     OAuthParameters parameters = new OAuthParameters 
     { 
      ConsumerKey = kConsumerKey, 
      ConsumerSecret = kConsumerSecret 
     }; 

     // check to see if we have saved tokens and set 
     var tokens = (from a in context.GO_GoogleAuthorizeTokens where a.id = id select a); 
     if (tokens.Count() > 0) 
     { 
      GO_GoogleAuthorizeToken token = tokens.First(); 
      parameters.Token = token.Token; 
      parameters.TokenSecret = token.TokenSecret; 
     } 

     // now build the factory 
     return new GOAuthRequestFactory("somevalue", kApplicationName, parameters); 
    } 

एक बार मैं अनुरोध कारखाना है, मैं विभिन्न एपीआई कि मैं का उपयोग करें और इस तरह से कुछ करने के लिए अनुमति है में से एक कॉल कर सकते हैं:

// authenticate to the google calendar 
CalendarService service = new CalendarService(kApplicationName); 
service.RequestFactory = GetGoogleOAuthFactory([user id]); 

// add from google doc record 
EventEntry entry = new EventEntry(); 
entry.Title.Text = goEvent.Title; 
entry.Content.Content = GoogleCalendarEventDescription(goEvent); 

When eventTime = new When(goEvent.StartTime, goEvent.EndTime.HasValue ? goEvent.EndTime.Value : DateTime.MinValue, goEvent.AllDay); 
entry.Times.Add(eventTime); 

// add the location 
Where eventLocation = new Where(); 
eventLocation.ValueString = String.Format("{0}, {1}, {2} {3}", goEvent.Address, goEvent.City, goEvent.State, goEvent.Zip); 
entry.Locations.Add(eventLocation); 

Uri postUri = new Uri(kCalendarURL); 

// set the request and receive the response 
EventEntry insertedEntry = service.Insert(postUri, entry); 
+0

क्या आपके पास कहीं भी पोस्ट किया गया कोई समाधान है? – Micah

+1

मेरे पास कोई समाधान नहीं है, लेकिन मैंने Google के ओथ से कनेक्ट होने के विवरण के साथ एक और धागे में पोस्ट किया है, जो आपकी मदद करता है। http://stackoverflow.com/a/7854919/947898 – uadrive

+0

धन्यवाद! बहुत ही सराहनीय! – Micah

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