我试图在asp.net c#项目中使用Google API登录Google表格,但出现redirect_uri_mismatch
错误400。我什至没有设置Google重定向到的重定向URL。我设置了重定向URL http://127.0.0.1:63092/login.aspx
,但是Google重定向到http://127.0.0.1:13557/authorize/
错误的端口和错误的网页。我的Credential.json
也没有/authorize/
之类的东西,如下所示。
{
"web": {
"client_id": "client_idxxxxxxx.apps.googleusercontent.com",
"project_id": "app-xxxxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "secret_xxxxxxxx",
"redirect_uris": [ "http://127.0.0.1:63092/login.aspx" ],
"javascript_origins": [ "http://127.0.0.1:63092" ]
}
}
每次尝试时,它都会更改127.0.0.1
上的端口并重定向到/authorize/
,这可能是什么问题?我在做什么错了?
我的代码
static string[] Scopes = { SheetsService.Scope.Spreadsheets };
static string ApplicationName = "My App";
static string thisDir = System.Web.Hosting.HostingEnvironment.MapPath("~/");
protected void Button1_Click(object sender, EventArgs e)
{
UserCredential credential;
using (var stream =
new FileStream(thisDir + "credential.json", FileMode.Open, FileAccess.Read))
{
string credPath = thisDir + "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
String spreadsheetId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String range = "Sheet1";
SpreadsheetsResource.ValuesResource.GetRequest request =
service.Spreadsheets.Values.Get(spreadsheetId, range);
ValueRange response = request.Execute();
IList<IList<Object>> values = response.Values;
if (values != null && values.Count > 0)
{
Console.WriteLine("Name, Major");
foreach (var row in values)
{
Console.WriteLine("{0}, {1}", row[0], row[4]);
}
}
else
{
Console.WriteLine("No data found.");
}
}
答案 0 :(得分:1)
Google提供了用于不同应用程序使用的服务器凭据类型。对于您而言,您使用的是为Web服务器设计的凭据,而不是为已安装的应用程序(在某人的桌面上本地运行)设计的凭据。
在Google凭据控制台中,为“应用程序类型”选择“其他”。下载新的凭据,然后重试。