亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

谷歌“GoogleWebAuthorizationBroker”顯示錯誤重定向 uri 不匹配

谷歌“GoogleWebAuthorizationBroker”顯示錯誤重定向 uri 不匹配

C#
慕的地6264312 2022-10-23 13:28:01
我在 MVC (C#) 中做這件事。我想訪問用戶谷歌日歷,所以我指定了一個帶有“訪問日歷”的按鈕。當用戶單擊下面的按鈕時,將調用代碼以獲取令牌(并保存)以訪問日歷數據。 UserCredential credential;                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(                    new ClientSecrets                    {                        ClientId = "xxxxxx.apps.googleusercontent.com",                        ClientSecret = "FxxxxxxxxxZ"                    },                    Scopes,                    "user",                    CancellationToken.None,                    new FileDataStore(credPath)).Result;當這個方法被執行時,我們應該被重定向到同意屏幕,相反,我得到的錯誤是但它顯示的重定向 URI 我從未在控制臺中指定。這些是我在谷歌項目控制臺中指定的重定向 uri。我做錯了什么嗎?如何正確重定向到權限屏幕?
查看完整描述

1 回答

?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

重定向uri問題

您請求中的重定向 uri 是http://127.0.1:59693/authorize您尚未在授權重定向 Uris 下添加的。

您不能只添加任何重定向 uri??蛻舳藥熳约簶嫿ㄟ@個 url??偸侨绱?/p>

host:port/authorize

申請類型

您可以創建多種類型的客戶端,這些客戶端旨在與不同類型的應用程序一起使用。用于連接這些客戶端的代碼也不同。

  • 已安裝的應用程序 - 安裝在用戶機器上的應用程序

  • Web 應用程序 - 托管在通過 Web 瀏覽器連接到用戶的 Web 服務器中的應用程序。

已安裝的應用程序

您正在使用GoogleWebAuthorizationBroker.AuthorizeAsync它是為與已安裝的應用程序一起使用而設計的。瀏覽器窗口將在機器本身上打開。如果您嘗試將其托管在 Web 服務器上,瀏覽器將嘗試在服務器上打開并且不會顯示給用戶。

網絡應用

您應該關注Web 應用程序并使用GoogleAuthorizationCodeFlow

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; }

        }

    }

}


查看完整回答
反對 回復 2022-10-23
  • 1 回答
  • 0 關注
  • 101 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號