开发者

Adding Google Calendar entry without using setUserCredentials

开发者 https://www.devze.com 2023-03-04 18:28 出处:网络
I am following the example provided by Google for Market place app at http://code.google.com/googleapps/marketplace/tutorial_dotnet.html

I am following the example provided by Google for Market place app at

http://code.google.com/googleapps/marketplace/tutorial_dotnet.html

I got the google authentication working as in the example , My next task is to add a entry to Google calendar. I found following code for that, and it is also working fine

CalendarService service = new CalendarService(APPLICATION_NAME);


            service.setUserCredentials(vUserName, vPassword);

            Google.GData.Calendar.EventEntry entry = new Google.GData.Calendar.EventEntry();

            // Set the title and content of the entry.
            entry.Title.Text = title;
            entry.Content.Content = contents;

            // Set a location for the event.
            Where eventLocation = new Where();
            eventLocation.ValueString = location;
            entry.Locations.Add(eventLocation);

            When eventTime = new When(startTime, endTime);
            entry.Times.Add(eventTime);

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

            // Send the request and receive the response:
            AtomEntry insertedEntry = service.Insert(postUri, entry);

The problem i have is the following line, If i give my username and password it will work

service.setUserCredentials(vUserName, vPassword);

i have authenticated the user as in google example. So I don’t know the username and password of other users login to my site using their gmail.

How do i add a calender entry with the information i have?

I have seen several exam开发者_运维技巧ples with RequestFactory authenticating the user. but couldn't find complete example that I can use


  1. you will need to create a .pfx cert file and upload it to google and place it on your server.
  2. create your AuthSubRequest URL

    <asp:HyperLink ID="GotoAuthSubLink" runat="server"/>
    GotoAuthSubLink.Text = "Login to your Google Account"; 
    GotoAuthSubLink.NavigateUrl = AuthSubUtil.getRequestUrl("(return url)http://www.example.com/RetrieveToken", "https://www.google.com/calendar/feeds/", false, true);
    
  3. after the person clicks on your auth link they are returned to your return url. get your session token as follows

    String sessionToken = ""; //Save this for making your calls.
    String certFile = "D:\\websites\\yourwebsite.com\\google.pfx";
    String result = GetAuthSubSessionToken(Request["token"]);
    
    protected AsymmetricAlgorithm GetRsaKey()
    {
        X509Certificate2 cert = new X509Certificate2(certFile, ""); 
        RSACryptoServiceProvider privateKey = cert.PrivateKey as RSACryptoServiceProvider;
        return privateKey;
    }
    
    public string GetAuthSubSessionToken(string singleUseToken)
    {
        string gatStr = "";
        try
        {
            AsymmetricAlgorithm rsaKey = GetRsaKey();
            try
            {
                sessionToken = AuthSubUtil.exchangeForSessionToken(singleUseToken, rsaKey).ToString();
                gatStr = "Session Token = " + SessionToken;
            }
            catch (Exception e)
            {
                gatStr = "Error: I appears that the Google authentication server is experiencing an error. Try the authorizaton link again in a few minutes. <a href=\""
                         + rtnUrl + "\" title=\"" + e.Message + "\">continue</a>";
            }
        }
        catch (Exception E)
        {
            gatStr = "Error: rsa " + E.Message + E.StackTrace;
        }
        return gatStr;
    }
    
  4. save the session token and use CreateCalendarService in subsequent calls to create your calendar service.

    public CalendarService CreateCalendarService()
    {
        GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("cl", "YourName-calendarApp-1");
        authFactory.Token = sessionToken;
        authFactory.PrivateKey = GetRsaKey();
        CalendarService cs = new CalendarService(authFactory.ApplicationName);
        cs.RequestFactory = authFactory;
        return cs;
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消