前言:

google提供了很多的服务,包括calendar, picasa, doc等,这些服务都开放了API,这样开发者可以无缝的把google的服务集成到自己的网站。

比如,网站图片没空间放,通过调用google的api就可以把图片放在google里面了。

正文:

首先,肯定是下载google提供的.net版本API,我就打个包放上来了,在vs2005里面添加引用就可以了。具体可以参考code.google.com.

 

下载:

点击下载

 

要使用Google提供的API,首先过验证关。

Google验证包括:ClientLogin and AuthSub.

前者就是用个人的用户名、密码登录,适合单机系统(如果你的网站只操作你的Google也行)

后者有点像支付宝提供的服务,即你的网站向google发出请求,跳转到google的登录页面,用户输入登录资料,google跳转回你的网站获得返回的token,然后下一步操作。

先说说后者AuthSub的Quick Start:

http://code.google.com/support/bin/answer.py?answer=75506

直接看页面最后的代码就可以了。这里有个要注意,如果是本机测试,那么vs2005使用localhost作为地址,google跳转回来的时候会出现安全提示。

 


    void PrintCalendar() {

        GAuthSubRequestFactory authFactory 
= new GAuthSubRequestFactory("cl""TesterApp");
        authFactory.Token 
= (String) Session["token"];
        CalendarService service 
= new CalendarService(authFactory.ApplicationName);
        service.RequestFactory 
= authFactory;

        EventQuery query 
= new EventQuery();

        query.Uri 
= new Uri("http://www.google.com/calendar/feeds/default/private/full");

        
try
        {
            EventFeed calFeed 
= service.Query(query);
            foreach (Google.GData.Calendar.EventEntry entry 
in calFeed.Entries)
            {
                Response.Write(
"Event: " + entry.Title.Text + "<br/>");
            }
        }
        
catch (GDataRequestException gdre)
        {
            HttpWebResponse response 
= (HttpWebResponse)gdre.Response;
            
            
//bad auth token, clear session and refresh the page
            if (response.StatusCode == HttpStatusCode.Unauthorized)
            {
                Session.Clear();
                Response.Redirect(Request.Url.AbsolutePath, 
true);
            }
            
else
            {
                Response.Write(
"Error processing request: " + gdre.ToString());
            }
        }
    }

</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Test Site</title>
</head>
<body>

    
<form id="form1" runat="server">
    
<h1>AuthSub Sample Page</h1>
    
<div>
    
<%
        GotoAuthSubLink.Visible 
= false;
        
        
if (Session["token"] != null)
        {
            PrintCalendar();
        }
        
else if (Request.QueryString["token"] != null)
        {
            
String token = Request.QueryString["token"];
            Session[
"token"= AuthSubUtil.exchangeForSessionToken(token, null).ToString();
            Response.Redirect(Request.Url.AbsolutePath, 
true);
        }
        
else //no auth data, print link
        {
            GotoAuthSubLink.Text 
= "Login to your Google Account";
            GotoAuthSubLink.Visible 
= true;
            GotoAuthSubLink.NavigateUrl 
= AuthSubUtil.getRequestUrl(Request.Url.ToString(),
                
"http://www.google.com/calendar/feeds/",false,true);
        }
        
     
%>
    
<asp:HyperLink ID="GotoAuthSubLink" runat="server"/>

    
</div>
    
</form>
</body>
</html>

 

这个AuthSub是个高阶的东西,以后再慢慢用,现在用ClientLogin就行了。

PicasaService myService = new PicasaService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo@gmail.com", "mypassword");

 

参考上文,这样就获得的service,然后就可以继续你的程序逻辑了。

 

小结:

google有2种认证,

clientlogin使用用户名、密码直接初始化xxxservice然后进行操作,

authsub认证通过用户自登陆,网站获得google返回的token然后生成xxxservice,然后继续操作。

参考文献:

http://code.google.com/apis/picasaweb/developers_guide_dotnet.html

相关文章:

  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
  • 2021-09-23
猜你喜欢
  • 2022-12-23
  • 2022-02-11
  • 2021-12-24
  • 2022-02-26
  • 2022-01-08
  • 2021-11-08
  • 2021-12-20
相关资源
相似解决方案