无权列出 Google 个具有范围 OAuth 令牌的电子表格

Unauthorized to list Google spreadsheets with scoped OAuth token

我正在 Windows 桌面上制作程序 运行ning。我很确定我将应用程序类型注册为 "Installed application" - 这有关系吗?

我可以从用户那里获取访​​问代码并将其与我的客户端 ID 和密码结合起来以获得访问令牌。我可以将此访问令牌用于 运行 一些请求用户联系人的示例代码,但是当我尝试列出他们的电子表格(使用示例代码)时,它失败了:Execution of request failed: https://spreadsheets.google.com/feeds/spreadsheets/private/fullThe remote server returned an error: (401) Unauthorized

我认为范围是正确的,就我程序的 API 而言,我启用了联系人 API、驱动器 API 和驱动器 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);
    }
}

原来授权请求​​的方式不是调用SetAuthenticationToken;有必要创建一个 GOAuth2RequestFactory 并将其添加到服务中:

SpreadsheetsService service = new SpreadsheetsService("Ribbon");
GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "Ribbon", googleAuth);
service.RequestFactory = requestFactory;

我的印象是我会打电话给工厂,因此我没有使用它,因为我不知道如何使用它。

您的 OAuth 工作正常意味着您已正确配置 Google 应用程序。 管理 SDK 也发生了类似的问题,它与不正确的 SCOPES 有关。

试试这个范围(这个范围末尾没有“/”.../feeds,而不是.../feeds/),Detailed example

string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";

也可以使用 API explorer 查看确切的范围信息。