Google api Google OAuth Api登录时未重定向
我尝试使用谷歌认证服务对我的用户进行认证 当我在本地服务器上运行这段代码时,它工作正常(它重定向到google登录,成功登录后在重定向路径上点击回调)。 但当在生产服务器上发布此代码时,它将不起作用。 当我调试这段代码时,我找到了它的重定向,并在托管环境(应用程序发布的地方)上打开google登录页面 这是我的代码-请帮忙 字符串路径=” 如果你需要更多信息,请告诉我。Google api Google OAuth Api登录时未重定向,google-api,google-oauth,youtube-data-api,google-api-dotnet-client,Google Api,Google Oauth,Youtube Data Api,Google Api Dotnet Client,我尝试使用谷歌认证服务对我的用户进行认证 当我在本地服务器上运行这段代码时,它工作正常(它重定向到google登录,成功登录后在重定向路径上点击回调)。 但当在生产服务器上发布此代码时,它将不起作用。 当我调试这段代码时,我找到了它的重定向,并在托管环境(应用程序发布的地方)上打开google登录页面 这是我的代码-请帮忙 字符串路径=” 如果你需要更多信息,请告诉我。 提前感谢从网页登录的代码与使用已安装的应用程序登录的代码不同。已安装的应用程序可以直接在当前计算机上生成登录屏幕。如果您试图在
提前感谢从网页登录的代码与使用已安装的应用程序登录的代码不同。已安装的应用程序可以直接在当前计算机上生成登录屏幕。如果您试图在web服务器上执行此操作,它将不起作用。下面是使用web登录的代码
using System;
using System.Web.Mvc;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Drive.v2;
using Google.Apis.Util.Store;
namespace Google.Apis.Sample.MVC4
{
public class AppFlowMetadata : FlowMetadata
{
private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "PUT_CLIENT_ID_HERE",
ClientSecret = "PUT_CLIENT_SECRET_HERE"
},
Scopes = new[] { DriveService.Scope.Drive },
DataStore = new FileDataStore("Drive.Api.Auth.Store")
});
public override string GetUserId(Controller controller)
{
// In this sample we use the session to store the user identifiers.
// That's not the best practice, because you should have a logic to identify
// a user. You might want to use "OpenID Connect".
// You can read more about the protocol in the following link:
// https://developers.google.com/accounts/docs/OAuth2Login.
var user = controller.Session["user"];
if (user == null)
{
user = Guid.NewGuid();
controller.Session["user"] = user;
}
return user.ToString();
}
public override IAuthorizationCodeFlow Flow
{
get { return flow; }
}
}
}
复制自Hi,我已尝试使用实际重定向路径并使用字符串redirecrPath=“”;确保在google developer中设置了corect重定向uriconsole@DaImTo在生产uri路径的情况下,它在服务器机器上而不是在客户端机器上打开,本地主机和您的服务器是两个不同的位置。本地主机将被删除。服务器将是其不同的重定向URI。您似乎也在为已安装的客户端使用代码。您应该使用web代码。谢谢您的回答,我已经实现了web代码,并且工作正常。@Dalm能否请您解释一下DataStore=new FileDataStore(“Drive.Api.Auth.Store”);我很高兴它为你工作记住接受anwser@Danish你应该问第二个问题,但我这里有一个教程,我这里也有一堆数据存储
using System;
using System.Web.Mvc;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Drive.v2;
using Google.Apis.Util.Store;
namespace Google.Apis.Sample.MVC4
{
public class AppFlowMetadata : FlowMetadata
{
private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "PUT_CLIENT_ID_HERE",
ClientSecret = "PUT_CLIENT_SECRET_HERE"
},
Scopes = new[] { DriveService.Scope.Drive },
DataStore = new FileDataStore("Drive.Api.Auth.Store")
});
public override string GetUserId(Controller controller)
{
// In this sample we use the session to store the user identifiers.
// That's not the best practice, because you should have a logic to identify
// a user. You might want to use "OpenID Connect".
// You can read more about the protocol in the following link:
// https://developers.google.com/accounts/docs/OAuth2Login.
var user = controller.Session["user"];
if (user == null)
{
user = Guid.NewGuid();
controller.Session["user"] = user;
}
return user.ToString();
}
public override IAuthorizationCodeFlow Flow
{
get { return flow; }
}
}
}