哎呀,对不起,我第一次没把它弄好。我在一个真正的应用程序中使用这段代码,我只是在代码中做了一些不同的事情,因为我经常刷新令牌。
在任何情况下,这里的正确的逻辑:
// 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
领域。如果您存储这两个值,则可以在随后的调用中将它们设置为参数结构(允许您跳过将来要求授权的情况),而不是致电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);
}
这就像所有其他职位似乎这部分掩盖://现在使用的URL授权在浏览器中的应用,并得到访问代码 (...) 这甚至意味着什么? – 2013-08-07 04:05:14
此示例未使用OAuth2Parameters:http://dotnetoday.blogspot.com.es/2012/10/c-google-api-getting-google-contacts.html – Kiquenet 2013-12-26 14:28:08