ASP.NETMVCC#GoogleDriveAPI重定向URI不匹配
创始人
2024-09-19 04:30:39
0

在Google开发者控制台中,配置的“重定向URI”必须与使用时C#代码中所提供的“重定向URI”相同。

以下是一个示例,用于从Google Drive API获取access_token:

public class HomeController : Controller
{
    private static readonly string CLIENT_ID = "your-client-id";
    private static readonly string CLIENT_SECRET = "your-client-secret";
    private static readonly string REDIRECT_URI = "your-redirect-uri";
    
    private static readonly string[] SCOPES = { DriveService.Scope.DriveReadonly };
    
    public ActionResult Index()
    {
        UserCredential credential;
        using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                SCOPES,
                "user",
                CancellationToken.None,
                new FileDataStore("Drive.Auth.Store")).Result;
        }
        
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "ASP.NET MVC Google Drive API Sample"
        });
        
        // do something with service
        
        return View();
    }
    
    public ActionResult Auth()
    {
        var uri = new Uri(REDIRECT_URI);
        var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = CLIENT_ID,
                ClientSecret = CLIENT_SECRET
            },
            Scopes = SCOPES
        });
        
        var code = Request.QueryString["code"];
        if (code == null)
        {
            var state = Guid.NewGuid().ToString();
            Session["state"] = state;
            var authUrl = flow.CreateAuthorizationCodeRequest(uri).Build();
            return Redirect(authUrl + "&state=" + state);
        }
        else
        {
            var token = flow.ExchangeCodeForTokenAsync("user", code, uri).Result;
            Session["access_token"] = token.AccessToken;
            Session["refresh_token"] = token.RefreshToken;
            return RedirectToAction("Index");
        }
    }
}

在上述代码示例

相关内容

热门资讯

一起来探讨"好友赣南... 一起来探讨"好友赣南脚本插件"开挂(透视)辅助平台(原来确实是有挂的)-哔哩哔哩;1、完成好友赣南脚...
玩家攻略推荐!乐乐游戏辅助器,... 玩家攻略推荐!乐乐游戏辅助器,微友辅助器免费版v2.0,2026新版技巧(有挂存在)-哔哩哔哩微友辅...
一起来讨论"指尖四川... 一起来讨论"指尖四川修改器"开挂(透视)辅助插件(一直是真的有挂)-哔哩哔哩;人气非常高,ai更新快...
玩家交流!九九山城万州版脚本,... 玩家交流!九九山城万州版脚本,四川熊猫辅助软件,力荐教程(有挂教学)-哔哩哔哩九九山城万州版脚本ai...
如何分辨真伪"决战卡... 如何分辨真伪"决战卡五星辅助神器"开挂(透视)辅助脚本(果然是有挂)-哔哩哔哩;决战卡五星辅助神器是...
透视代打!微信边锋辅助挂件,w... 微信边锋辅助挂件开挂教程视频分享装挂详细步骤在当今的网络游戏中,微信边锋辅助挂件作为一种经典的娱乐方...
透视透视"新上游拼十... 透视透视"新上游拼十辅助"开挂(透视)辅助安装(一贯确实有挂)-哔哩哔哩;亲真的是有正版授权,小编(...
最新通报!吉祥填大坑插件,we... 最新通报!吉祥填大坑插件,wepoker透视最简单三个步骤,扑克教程!(了解有挂)-哔哩哔哩;无需打...
推荐几款新版"多乐小... 推荐几款新版"多乐小程序辅助工具"开挂(透视)辅助安装(一贯是真的有挂)-哔哩哔哩;AI辅助机器人普...
我来教大家"欢乐达人... 我来教大家"欢乐达人暗宝怎么获得"开挂(透视)辅助软件(果然确实是有挂)-哔哩哔哩;欢乐达人暗宝怎么...