Google云端硬盘简单API访问授权失败

时间:2012-12-06 11:21:17

标签: c# .net oauth-2.0 google-drive-api

我正在尝试使用提供的.NET SDK在google中进行授权,但它会因“无效凭据”错误而失败。我找到了这个答案

Google Calendar V3 2 Legged authentication fails

但我仍然没有看到错误在哪里。看起来一切都按照描述完成。

这是我的代码

const string CONSUMER_KEY = "mytestdomain.com";
const string CONSUMER_SECRET = "my_consumer_secret";
const string TARGET_USER = "user";
const string SERVICE_KEY = "some_api_key";

var auth = new OAuth2LeggedAuthenticator(CONSUMER_KEY, CONSUMER_SECRET, TARGET_USER, CONSUMER_KEY);

var service = new DriveService(auth) { Key = SERVICE_KEY };
var results = service.Files.List().Fetch();

Console.WriteLine(results.Items.Count);

这是谷歌控制面板的截图。我刚刚替换了domain nameconsumer keyapi key

Manage API client access页面的屏幕截图 Screenshot from "Manage API client access" page

Manage OAuth key and secret for this domain页面的屏幕截图 Screenshot from "Manage OAuth key and secret for this domain" page

API AccessGoogle API console页面的屏幕截图 Screenshot from "API Access" page in "Google API console"

1 个答案:

答案 0 :(得分:1)

OAuth 1.0已被弃用,并且使用了2-legged OAuth 1.0。虽然如果我是你,我仍然支持弃用期,但我会使用OAuth 2.0服务帐户来执行域范围的授权。

Google Drive SDK文档的Perform Google Apps Domain-wide Delegation of Authority页面上有详细记录。