.net Google/OAuth 2-自动登录

.net Google/OAuth 2-自动登录,.net,oauth-2.0,google-tasks-api,.net,Oauth 2.0,Google Tasks Api,我正在使用OAuth2.0和一些Google API。虽然授权过程非常简单,但在初始授权完成后,我面临自动授权的问题 因此: 在第4点,我有一个refresh_令牌,所以我应该使用该request_令牌请求一个新令牌。但我仍然不断收到401个未经授权的电话结果 因此,我尝试做的是,我的应用程序可以静默登录,这样用户就不必每次都授予访问权限。您应该能够使用以下请求刷新OAuth 2.0令牌: POST /o/oauth2/token HTTP/1.1 Host: accounts.google.c

我正在使用OAuth2.0和一些Google API。虽然授权过程非常简单,但在初始授权完成后,我面临自动授权的问题

因此:

在第4点,我有一个refresh_令牌,所以我应该使用该request_令牌请求一个新令牌。但我仍然不断收到401个未经授权的电话结果


因此,我尝试做的是,我的应用程序可以静默登录,这样用户就不必每次都授予访问权限。

您应该能够使用以下请求刷新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
正如上文所指出的

我刚刚用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}

我在.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);
    }
一旦我有了请求工厂,我就可以调用我有权使用的各种api之一,并执行如下操作:

// 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);

我仍然不知道我做错了什么;但不知怎么的,它现在起作用了!谢谢你有没有关于这篇文章的解决方案?我没有解决方案,但我在另一个帖子中发布了连接到谷歌Oauth的详细信息,如果这对你有帮助的话。
// 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);