【发布时间】:2015-03-13 18:55:49
【问题描述】:
我正在制作一个在 Windows 桌面上运行的程序。我很确定我将应用程序类型注册为“已安装的应用程序”——这有关系吗?
我可以从用户那里获取访问代码,并将其与我的客户端 ID 和密码结合起来以获取访问令牌。我可以使用此访问令牌运行一些请求用户联系人的示例代码,但是当我尝试列出他们的电子表格(使用示例代码)时,它失败了:Execution of request failed: https://spreadsheets.google.com/feeds/spreadsheets/private/full、The remote server returned an error: (401) Unauthorized
我认为范围是正确的,并且就我的程序的 API 而言,我启用了 Contacts API、Drive API 和 Drive SDK(我认为我不需要这个)。
这是相关的 C# 代码,如果您能解释为什么列出电子表格的请求失败,请提前感谢 - 我已经指出了发生这种情况的点:
using Google.GData.Client;
using Google.GData.Spreadsheets;
using Google.Contacts;
using Google.GData.Apps;
…
private static OAuth2Parameters googleAuth;
…
string CLIENT_ID = …;
string CLIENT_SECRET = …;
string SCOPE = "https://spreadsheets.google.com/feeds/ https://docs.googleusercontent.com/ https://docs.google.com/feeds/ https://www.google.com/m8/feeds/"; // read, write, create/delete sheets, use contacts
string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
…
googleAuth = new OAuth2Parameters();
googleAuth.ClientId = CLIENT_ID;
googleAuth.ClientSecret = CLIENT_SECRET;
googleAuth.RedirectUri = REDIRECT_URI;
googleAuth.Scope = SCOPE;
googleAuth.ResponseType = "code";
…
string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(googleAuth);
…
googleAuth.AccessCode = PaneManager.googleCode; // read it back from user (PaneManager is UI)
OAuthUtil.GetAccessToken(googleAuth);
…
RunContactsSample(googleAuth); // runs fine
SpreadsheetsService service = new SpreadsheetsService("Ribbon"); // does this name have any relevance?
service.SetAuthenticationToken(googleAuth.AccessToken);
// Instantiate a SpreadsheetQuery object to retrieve spreadsheets.
SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = service.Query(query); // this fails
// Iterate through all of the spreadsheets returned
foreach (SpreadsheetEntry entry in feed.Entries)
{
// Print the title of this spreadsheet to the screen
Console.WriteLine(entry.Title.Text);
}
private static void RunContactsSample(OAuth2Parameters parameters)
{
try
{
RequestSettings settings = new RequestSettings("Ribbon", parameters);
ContactsRequest cr = new ContactsRequest(settings);
Feed<Contact> f = cr.GetContacts();
foreach (Contact c in f.Entries)
{
Console.WriteLine(c.Name.FullName);
}
}
catch (AppsException a)
{
Console.WriteLine("A Google Apps error occurred.");
Console.WriteLine();
Console.WriteLine("Error code: {0}", a.ErrorCode);
Console.WriteLine("Invalid input: {0}", a.InvalidInput);
Console.WriteLine("Reason: {0}", a.Reason);
}
}
【问题讨论】:
-
您使用的是最新的客户端库吗? nuget.org/packages/Google.GData.Spreadsheets
-
是的,我昨天使用 Nuget 包管理器在我的项目中安装了这些库
-
听起来像是 SCOPES 问题,您是否尝试过任何其他服务,例如单元格提要等......所以你的范围和我的一样,但我不使用列表电子表格服务。
-
嗯,是的@eddyparkinson,在尝试指向我自己的一张工作表的 URL 的 WorksheetQuery 时,也因为未经授权而失败。我看到here 说“虽然 OAuth 协议支持桌面/已安装的应用程序用例,但 Google 只支持 Web 应用程序的 OAuth。” - 考虑到列出联系人的请求工作正常,这是否相关?
-
@kokociel 抱歉,不知道。我在这个 code.google.com/apis/console 上使用“Web 应用程序的客户端 ID”
标签: c# oauth-2.0 google-docs-api google-spreadsheet-api google-api-dotnet-client