0

我正在尝试为沙盒测试帐户集成 paypal api。当我尝试从请求代码中获取访问令牌时,我收到 400 错误请求错误。

cshtml: Goto paypal Js: function GetPaypal() { window.location = " https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=clientid&response_type=code&scope=Email&redirect_uri=http: //localhost:4427/ "; }

C# 代码:我正在使用以下代码获取代码。:

公共 ActionResult 索引(字符串代码)

{
        if (code != null)
        { 
            string postcontents = string.Format("client_id={0}&client_secret={1}&grant_type=authorization_code&redirect_uri={2}&code={3}"
                                      , System.Web.HttpUtility.UrlEncode("client_id")
                                      , System.Web.HttpUtility.UrlEncode("Client Secret")
                                      , System.Web.HttpUtility.UrlEncode("http://localhost:4427/")
                                      , System.Web.HttpUtility.UrlEncode(code));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice");
            request.Method = "POST";
            byte[] postcontentsArray = System.Text.Encoding.UTF8.GetBytes(postcontents);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postcontentsArray.Length;
            //OAuth.
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(postcontentsArray, 0, postcontentsArray.Length);
                requestStream.Close();
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    string responseFromServer = reader.ReadToEnd();
                    reader.Close();
                    responseStream.Close();
                    response.Close();
                    // return SerializeToken(responseFromServer);
                    dynamic dynObj = JsonConvert.DeserializeObject(responseFromServer);
                    string token = dynObj["access_token"];
                    //token = ser.Deserialize<ImportContacts._Default.GoogleOAuthToken>(responseFromServer);
                }
            }
        }
        return View();

}

4

1 回答 1

1

请更改您的 CSHTML 网址,如下所示:-

 <script type="text/javascript">
        function GetGmailContacts() {
            window.location = "https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id=clientid&response_type=code+id_token&scope=openid profile email&redirect_uri=localhost_url";
        }  
  </script>

C#代码:-

public ActionResult Index(string code,string id_token)
  {
        if (code != null)
        { 
            string postcontents = string.Format("client_id={0}&client_secret={1}&grant_type=authorization_code&redirect_uri={2}&code={3}"
                                      , System.Web.HttpUtility.UrlEncode("client_id")
                                      , System.Web.HttpUtility.UrlEncode("Client Secret")
                                      , System.Web.HttpUtility.UrlEncode(localhost_url)
                                      , System.Web.HttpUtility.UrlEncode(code));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice");
            request.Method = "POST";
            byte[] postcontentsArray = System.Text.Encoding.UTF8.GetBytes(postcontents);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postcontentsArray.Length;
            //OAuth.
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(postcontentsArray, 0, postcontentsArray.Length);
                requestStream.Close();
                WebResponse response = request.GetResponse();
                using (Stream responseStream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    string responseFromServer = reader.ReadToEnd();
                    reader.Close();
                    responseStream.Close();
                    response.Close();
                    // return SerializeToken(responseFromServer);
                    dynamic dynObj = JsonConvert.DeserializeObject(responseFromServer);
                    string token = dynObj["access_token"];
                    //token = ser.Deserialize<ImportContacts._Default.GoogleOAuthToken>(responseFromServer);
                }
            }
        }
        return View();
}

我希望它会奏效。

于 2013-09-08T05:22:46.620 回答