Google API Authentication

<pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;">
using System;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace GDrive {
class GDriveTest : Form {
public TextBox ctxtAuthURL, ctxtAccessCode;
public Label clblMessage, clblList;

public OAuth2Parameters cParams;

public static void Main(string[] astrArgs) {
GDriveTest myForm;

myForm = new GDriveTest();

Application.Run(myForm);
}

public GDriveTest() {
Button btnSubmit;

//Create the controls
clblMessage = new Label();
clblMessage.Height = 18;
clblMessage.Width = 300;
clblMessage.Top = 0;
clblMessage.Left = 0;

ctxtAuthURL = new TextBox();
ctxtAuthURL.Height = 18;
ctxtAuthURL.Width = 300;
ctxtAuthURL.Top = 20;
ctxtAuthURL.Left = 0;

ctxtAccessCode = new TextBox();
ctxtAccessCode.Height = 18;
ctxtAccessCode.Width = 300;
ctxtAccessCode.Top = 40;
ctxtAccessCode.Left = 0;

btnSubmit = new Button();
btnSubmit.Height = 18;
btnSubmit.Width = 50;
btnSubmit.Top = 60;
btnSubmit.Left = 0;
btnSubmit.Text = "Submit Code";
btnSubmit.Click += new EventHandler(SubmitAccessCode);

clblList = new Label();
clblList.Height = 200;
clblList.Width = 300;
clblList.Top = 80;
clblList.Left = 0;

//Position and configure the form
this.Width = 307;
this.Height = 307;
this.FormBorderStyle = FormBorderStyle.FixedSingle;

Controls.Add(clblMessage);
Controls.Add(ctxtAuthURL);
Controls.Add(ctxtAccessCode);
Controls.Add(btnSubmit);

//Create the authentication parameters
cParams = new OAuth2Parameters();

cParams.ClientId = "GOOGLE-CLIENT-ID";
cParams.ClientSecret = "GOOGLE-CLIENT-SECRET";
cParams.RedirectUri = "RED:IRE:CT:URI";
cParams.Scope = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
ctxtAuthURL.Text = OAuthUtil.CreateOAuth2AuthorizationUrl(cParams);
}

private void SubmitAccessCode(object oSender, EventArgs eaArgs) {
GOAuth2RequestFactory reqFactory;
SpreadsheetsService gsService;
SpreadsheetQuery sqQuery;
SpreadsheetFeed sfFeed;

cParams.AccessCode = ctxtAccessCode.Text;
OAuthUtil.GetAccessToken(cParams);

reqFactory = new GOAuth2RequestFactory(null, "", cParams);
gsService = new SpreadsheetsService("");
sqQuery = new SpreadsheetQuery();

gsService.RequestFactory = reqFactory;

sfFeed = gsService.Query(sqQuery);

clblList.Text = "";
foreach (SpreadsheetEntry seEntry in sfFeed.Entries) {
clblList.Text = clblList.Text + "\n" + seEntry.Title.Text;
}
}
}
}
</code></pre>

No comments:

Post a Comment