在将内容直接推送给用户的时候,电子邮件依然是最有力的工具之一,因此在很多营销战略中,电子邮件都扮演着重要的作用。为了让邮件营销发挥作用,许多客户端程序的开发者都会使用Google Contacts API来获取当前用户的联系人列表,从而找到更多潜在用户,最终提升流量。
与其他 Google Data APIs一样,这个系统会接受含有XML-encoded输入参数的HTTP请求,然后返回含有被请求信息的Atom feed,这些信息可以在任何XML-aware的编程语言中被解析。今天,我们就来了解以下如何使用OAuth 2.0验证,在C#和ASP.NET中部署Google Contacts API v3.0。
首先我们需要在Google APP Console中创建一个新的API项目-

在Google Console中创建项目
Google Console中的新项目
打开Google Contacts API
Google Console中的谷歌API
在左边栏中,点击“同意屏幕”。在打开的页面中输入电子邮件地址和产品名称。然后点击保存。
再一次回到左边栏,点击“凭据”。之后点击“创建新的客户端ID”。之后浏览器会打开新页面,程序类别请选择“网页应用”,然后输入“获得授权的重定向 URI”(也就是用户被带向的URL,用户需要在这里给应用授权)。最后点击“创建”,这一步会创建新的客户端ID和客户端密钥。保存好ID和密钥,我们将会在后面用上它们。
同意屏幕
同意屏幕字段
设定参数
创建新的客户端ID
创建客户端ID
OAuth 2.0会对用户的身份进行验证,并且向用户发送让应用获取他们的联系人信息的请求。如果用户接受了请求,谷歌就会给他发送验证码。
Google OAuth 2.0 架构
Google OAuth 2.0在线文档 – Using OAuth 2.0 for Web Server Applications
response_type – 代码
client_id – 你的客户端ID
scope – https://www.google.com/m8/feeds/&approval_prompt=force&access_type=offline
我们要以点击事件的方式完成这个步骤
HTML
<asp:Button ID="googleButton" Text="Get Google Contacts" runat="server" OnClick="googleButton_Click" />
C#
protected void googleButton_Click(object sender, EventArgs e)
{
string clientId = "ClientId";
string redirectUrl = "http://www.yogihosting.com/TutorialCode/GoogleContactAPI/google-contact-api.aspx";
Response.Redirect("https://accounts.google.com/o/oauth2/auth?redirect_uri="+redirectUrl+"&response_type=code&client_id=" + clientId + "&scope=https://www.google.com/m8/feeds/&approval_prompt=force&access_type=offline");
}
现在我们需要将HTTP POST请求指向https://accounts.google.com/o/oauth2/token,而且我们还需要pass下面的参数:
一个成功的响应会以JSON格式返回下列东西:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["code"] != null)
GetAccessToken();
}
它会调用GetAccessToken()函数:
public void GetAccessToken()
{
string code = Request.QueryString["code"];
string google_client_id = "292511778773-nktorr50e1palg9svl49825uqfqv9q0a.apps.googleusercontent.com";
string google_client_sceret = "sKuhzvMEN3-ye8BYWlP6hMm2";
string google_redirect_url = "http://www.yogihosting.com/TutorialCode/GoogleContactAPI/google-contact-api.aspx";
/*Get Access Token and Refresh Token*/
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
webRequest.Method = "POST";
string parameters = "code=" + code + "&client_id=" + google_client_id + "&client_secret=" + google_client_sceret + "&redirect_uri=" + google_redirect_url + "&grant_type=authorization_code";
byte[] byteArray = Encoding.UTF8.GetBytes(parameters);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = byteArray.Length;
Stream postStream = webRequest.GetRequestStream();
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
WebResponse response = webRequest.GetResponse();
postStream = response.GetResponseStream();
StreamReader reader = new StreamReader(postStream);
string responseFromServer = reader.ReadToEnd();
GooglePlusAccessToken serStatus = JsonConvert.DeserializeObject(responseFromServer);
/*End*/
GetContacts(serStatus);
}最后,这个函数会调用GetContacts()函数,其功能是调用Google API。
在从响应中获取JsonConvert类之后,我们将其放在GooglePlusAccessToken类中:
public class GooglePlusAccessToken
{
public string access_token { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public string refresh_token { get; set; }
}
请注意,在这一步中,为了调用API,我们需要使用一些Google dot net dll,它可以让我们的工作变得更简单。API调用的URL为https://www.google.com/m8/feeds/contacts/{userEmail}/full。
GetContacts()函数的定义:
public void GetContacts(GooglePlusAccessToken serStatus)
{
string google_client_id = "292511778773-nktorr50e1palg9svl49825uqfqv9q0a.apps.googleusercontent.com";
string google_client_sceret = "sKuhzvMEN3-ye8BYWlP6hMm2";
/*Get Google Contacts From Access Token and Refresh Token*/
string refreshToken = serStatus.refresh_token;
string accessToken = serStatus.access_token;
string scopes = "https://www.google.com/m8/feeds/contacts/default/full/";
OAuth2Parameters oAuthparameters = new OAuth2Parameters()
{
ClientId = google_client_id,
ClientSecret = google_client_sceret,
RedirectUri = "http://www.yogihosting.com/TutorialCode/GoogleContactAPI/google-contact-api.aspx",
Scope = scopes,
AccessToken = accessToken,
RefreshToken = refreshToken
};
RequestSettings settings = new RequestSettings("<var>YOUR_APPLICATION_NAME</var>", oAuthparameters);
ContactsRequest cr = new ContactsRequest(settings);
ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
query.NumberToRetrieve = 5000;
Feed feed = cr.Get(query);
StringBuilder sb = new StringBuilder();
int i = 1;
foreach (Contact entry in feed.Entries)
{
foreach (EMail email in entry.Emails)
{
sb.Append(i + " ").Append(email.Address).Append("
");
i++;
}
}
/*End*/
dataDiv.InnerHtml = sb.ToString();
}用户联系人数据是以JSON格式发送的,我们需要将其提取,然后对其进行loop,并且在一个div钟显示。
上面五部之后,Google Contacts API项目就完成了。

原文来自:SDK.cn
声明:所有来源为“聚合数据”的内容信息,未经本网许可,不得转载!如对内容有异议或投诉,请与我们联系。邮箱:marketing@think-land.com
Nano Banana(gemini-2.5-flash-image 和 gemini-3-pro-image-preview图像模型)是图像生成与编辑的最佳选择,可集成 Nano Banana API,实现高速预览。
支持通过自然语言文本智能生成高质量短视频。用户只需输入一段描述性文字,即可自动合成画面连贯、风格鲜明、配乐匹配的定制化视频内容。适用于短视频创作、广告预演、社交内容生成、游戏素材制作等场景,为开发者与创作者提供高效、灵活、富有想象力的视频生产新范式。
先进的图像理解和分析能力,它能够快速准确地解析和理解图像内容。无论是自然风景、城市建筑还是复杂的场景与活动,都能提供详细的描述和深入的分析。
根据文本提示(prompt)和图片公网访问链接,编辑原图按照特定风格、场景和氛围感的输出新的图像。广泛应用于电商营销、广告设计、创意灵感等领域,为用户带来高效且个性化的AI图像创作体验。
根据文本提示(prompt),生成生成具有特定风格、场景和氛围感的图像。广泛应用于电商营销、广告设计、创意灵感等领域,为用户带来高效且个性化的AI图像创作体验。