5

私はいくつかのGoogleAPIと組み合わせてOAuth2.0で少し遊んでいます。承認プロセスは非常に簡単ですが、最初の承認が完了した後の自動承認に問題があります。

それで:

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の許可されていない結果を取得し続けます。

だから私がやろうとしているのは、ユーザーが毎回アクセスを許可する必要がないように、アプリケーションがサイレントにログオンできるようにすることです。

4

2 に答える 2

2

次のリクエストを使用して、OAuth2.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

GoogleOAuth2.0のドキュメントで指摘されているように。

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}
于 2011-06-29T16:29:57.720 に答える
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);
    }

リクエストファクトリを取得したら、使用する権限があるさまざまなAPIの1つに電話して、次のようなことを行うことができます。

// 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);
于 2011-10-21T20:20:56.093 に答える