如何避免谷歌API访问代码或访问令牌的C#



我正在开发一个应用程序,将行插入到我的一个电子表格(如数据库(中。问题是,它总是询问新的访问代码。请帮助我避免访问代码过期,否则我只想更新我的谷歌驱动器帐户excel。有时会有一个简单的代码,我可以使用错误的代码吗?以下是我的代码:

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);

您可以在请求访问令牌时请求"脱机"访问。服务器将返回一个"刷新令牌",使用此令牌,您可以在新的"访问令牌"到期时请求它,而无需用户再次授予权限。

在这里你可以找到相关的文档。

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

相关内容

  • 没有找到相关文章

最新更新