zoukankan      html  css  js  c++  java
  • 关于PayPal第三方登录的接口问题

    按照官网上面的方法试了几次都会出现各种问题,于是稍作改动:

      首先,找到官网的SDK,注意版本,可能会出现NewTonSoft.Json版本不对的问题。

            public ActionResult Button() 
            {
                string redirectURI = "https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?client_id= aaa &response_type=code&scope=openid email&redirect_uri=http://xxx";
                return Redirect(redirectURI);
            }

      将aaa换成自己的client_id,xxx换成自己需要返回的方法,不需要加返回值;

            public void Receive(String code)
            {
                //填写自己的Client_ID
                var oAuthClientId = "aaa";
                //对应的Client_Secret
                var oAuthClientSecret = "bbb";
                var oAuthUrl = "https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice";
    
                var authHeader = string.Format("Basic {0}",
                                               Convert.ToBase64String(
                                                   Encoding.UTF8.GetBytes(Uri.EscapeDataString(oAuthClientId) + ":" +
                                                                          Uri.EscapeDataString((oAuthClientSecret)))
                                                   ));
    
                //passing code here
                var postBody = string.Format("grant_type=authorization_code&code={0}", code);
    var authRequest = (HttpWebRequest)WebRequest.Create(oAuthUrl);
                authRequest.Headers.Add("Authorization", authHeader);
                authRequest.Method = "POST";
                byte[] postcontentsArray = Encoding.UTF8.GetBytes(postBody);
                authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                authRequest.ContentLength = postcontentsArray.Length;
    
                try
                {
                    using (Stream stream = authRequest.GetRequestStream())
                    {
                        stream.Write(postcontentsArray, 0, postcontentsArray.Length);
                        stream.Close();
    
                        WebResponse response = authRequest.GetResponse();
                        using (Stream responseStream = response.GetResponseStream())
                            if (responseStream != null)
                            {
                                using (var reader = new StreamReader(responseStream))
                                {
                                    string responseFromServer = reader.ReadToEnd();
                                    reader.Close();
                                    responseStream.Close();
                                    response.Close();
                                    //this will return you access token which you can use to get user information
                                    //使用JsonSerializer读写对象(基于JsonWriter与JsonReader)
                                    var responseResult =
                                        (JObject)JsonConvert.DeserializeObject(responseFromServer);
                                    //var a1 = responseResult["access_token"].ToString();
                                    Dictionary<string, string> configurationMap = new Dictionary<string, string>();
                                    configurationMap.Add("mode", "sandbox");
    
                                    APIContext apiContext = new APIContext();
                                    apiContext.Config = configurationMap;
    
                                    UserinfoParameters userinfoParams = new UserinfoParameters();
                                    //传入参数access_token
                                    userinfoParams.SetAccessToken(responseResult["access_token"].ToString());
                                    //得到用户信息
                                    Userinfo userinfo = Userinfo.GetUserinfo(apiContext, userinfoParams);
                                }
                            }
                    }
                }
                catch (Exception e)
                {
                    //log error
                }
            }
        此时可能会出现:“请求被中止:未能创建SSL/TLS安全通道"的问题,在new HttpWebRequest之前加入以下代码可以解决此问题。
       System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
      默认状态下,得到的用户信息包含email和一个用户ID
  • 相关阅读:
    spring的9个地方调用了5次后置处理器的详细情况
    spring容器启动
    什么是好的代码
    随机文件读写
    mysql 写锁
    mysql 高效率查询背景
    spring中的重点
    spring bean生命周期和上下文初始化
    雷电模拟器 v3.71绿色版
    免费申请 QQ 免费靓号
  • 原文地址:https://www.cnblogs.com/tearfc/p/5337667.html
Copyright © 2011-2022 走看看