未经授权使用范围OAuth令牌列出Google电子表格

时间:2015-01-15 11:32:54

标签: c# oauth-2.0 google-docs-api google-spreadsheet-api google-api-dotnet-client

我正在Windows桌面上运行程序。我非常确定我将应用程序类型注册为"已安装的应用程序" - 无论如何这有关系吗?

我可以从用户那里获取访​​问代码,并将其与我的客户端ID和密码相结合,以获取访问令牌。我可以使用此访问令牌来运行请求用户联系人的示例代码,但是当我尝试列出他们的电子表格(使用示例代码)时,它会失败:Execution of request failed: https://spreadsheets.google.com/feeds/spreadsheets/private/fullThe 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);
    }
}

2 个答案:

答案 0 :(得分:1)

事实证明,授权请求的方式并不是调用SetAuthenticationToken;创建GOAuth2RequestFactory并将其添加到服务中是必要的:

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

我的印象是我会是那个叫工厂的人,因此我没有使用它,因为我不知道如何。

答案 1 :(得分:1)

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

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

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

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