おっと、すみません、最初はよくわかりませんでした。私は実際のアプリでこのコードを使用しています。トークンを常に更新しているため、コードで少し異なることを行います。
いずれにせよ、適切なロジックは次のとおりです。
// get this information from Google's API Console after registering your app
var parameters = new OAuth2Parameters
{
ClientId = @"",
ClientSecret = @"",
RedirectUri = @"",
Scope = @"https://www.google.com/m8/feeds/",
};
// generate the authorization url
string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
// now use the url to authorize the app in the browser and get the access code
(...)
// get this information from Google's API Console after registering your app
parameters.AccessCode = @"<from previous step>";
// get an access token
OAuthUtil.GetAccessToken(parameters);
// setup connection to contacts service
var contacts = new ContactsRequest(new RequestSettings("<appname>", parameters));
// get each contact
foreach (var contact in contacts.GetContacts().Entries)
{
System.Diagnostics.Debug.WriteLine(contact.ContactEntry.Name.FullName);
}
GetAccessToken()
参考までに、アクセス コードに対して呼び出した後、パラメーター データ構造にはAccessToken
およびRefreshToken
フィールドが含まれます。これら 2 つの値を STORE すると、後続の呼び出しでパラメーター構造にそれらを設定することができ (将来、承認の要求をスキップできます)、GetAccessToken()
単に呼び出す代わりRefreshAccessToken(parameters)
に、連絡先にいつでもアクセスできます。わかる?ここで、見てみましょう:
// get this information from Google's API Console after registering your app
var parameters = new OAuth2Parameters
{
ClientId = @"",
ClientSecret = @"",
RedirectUri = @"",
Scope = @"https://www.google.com/m8/feeds/",
AccessCode = "",
AccessToken = "", /* use the value returned from the old call to GetAccessToken here */
RefreshToken = "", /* use the value returned from the old call to GetAccessToken here */
};
// get an access token
OAuthUtil.RefreshAccessToken(parameters);
// setup connection to contacts service
var contacts = new ContactsRequest(new RequestSettings("<appname>", parameters));
// get each contact
foreach (var contact in contacts.GetContacts().Entries)
{
System.Diagnostics.Debug.WriteLine(contact.ContactEntry.Name.FullName);
}
編集:
// generate the authorization url
string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
// now use the url to authorize the app in the browser and get the access code
(...)
// get this information from Google's API Console after registering your app
var parameters = new OAuth2Parameters
{
ClientId = @"",
ClientSecret = @"",
RedirectUri = @"",
Scope = @"https://www.google.com/m8/feeds/",
AccessCode = @"<from previous step>",
};
// get an access token
OAuthUtil.GetAccessToken(parameters);
// setup connection to contacts service
var contacts = new ContactsRequest(new RequestSettings("<appname>", parameters));
// get each contact
foreach (var contact in contacts.GetContacts().Entries)
{
System.Diagnostics.Debug.WriteLine(contact.ContactEntry.Name.FullName);
}