如何通过C#

时间:2015-07-01 12:52:52

标签: c# google-api google-spreadsheet-api

我正在开发一个应用程序来将行插入我的电子表格之一(如数据库)。问题是,它总是要求新的访问代码。请帮我避免访问代码过期,或者我只想更新我的google云端硬盘帐户。有时会有一个简单的代码,我可能使用错误的代码?以下是我的代码:

string CLIENT_ID = "HIDE";
string CLIENT_SECRET = "HIDE";
string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

OAuth2Parameters parameters = new OAuth2Parameters();
parameters.ClientId = CLIENT_ID;
parameters.ClientSecret = CLIENT_SECRET;
parameters.RedirectUri = REDIRECT_URI;

parameters.Scope = SCOPE;
string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
auth.Text = authorizationUrl;
info.Text = ("Please visit the URL above to authorize your OAuth " +"request token.  Once that is complete, type in your access code to "+ "continue...");
parameters.AccessCode = accc.Text;

OAuthUtil.GetAccessToken(parameters);
string accessToken = parameters.AccessToken;
acc2.Text =  accessToken;

GOAuth2RequestFactory requestFactory =
                          new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
                      service.RequestFactory = requestFactory;
                      string USERNAME = "HIDE";
                      string PASSWORD = "HIDE";

                      service.setUserCredentials(USERNAME, PASSWORD);

                      SpreadsheetQuery query = new SpreadsheetQuery();

                      SpreadsheetFeed feed = service.Query(query);

                      if (feed.Entries.Count == 0)
                      {
                          Console.WriteLine("None");
                      }

                      SpreadsheetEntry spreadsheet = (SpreadsheetEntry)feed.Entries[0];
                      Console.WriteLine(spreadsheet.Title.Text);

                      WorksheetFeed wsFeed = spreadsheet.Worksheets;
                      WorksheetEntry worksheet = (WorksheetEntry)wsFeed.Entries[0];

                      AtomLink listFeedLink = worksheet.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null);


                      ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString());
                      ListFeed listFeed = service.Query(listQuery);

                      ListEntry row = new ListEntry();
                      row.Elements.Add(new ListEntry.Custom() { LocalName = "no.", Value = "#" });
                      service.Insert(listFeed, row);

1 个答案:

答案 0 :(得分:1)

您可以请求"离线"请求访问令牌时访问。服务器将返回一个"刷新令牌"并且通过这个方法,您可以申请一个新的访问令牌"当它过期而没有用户再次授予权限时。

Here您可以找到相关文档。

检查this教程,它将帮助您以编程方式获取访问令牌和刷新令牌,而不必打开浏览器并复制令牌。