2011-06-06 118 views
5

我在OAuth 2.0中結合一些Google API玩了一下。儘管授權過程非常簡單,但在初始授權完成後,我面臨自動授權問題。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指出。

我只是想出來使用curl,它按預期工作:

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 2011-06-30 07:36:44

1

我這樣做,在.NET中使用Google.GData.Client。一旦我完成了授權過程並保存了令牌,下次我的用戶訪問該站點時,我會通過生成一個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); 
    } 

一旦我有要求的工廠,我可以調用各種AP​​I的,我不得不使用和做這樣的事情的權限之一:

// 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 2011-12-06 21:23:21

+1

我沒有解決方案,但我確實在其他主題中發佈了連接到Google Oauth的詳細信息,如果這有助於您的話。 http://stackoverflow.com/a/7854919/947898 – uadrive 2011-12-06 22:13:15

+0

謝謝!非常感激! – Micah 2011-12-07 12:49:22