我正在尝试整合paypal api for sandbox测试帐户。当我尝试从请求代码中获取访问令牌时,我收到400错误请求错误。
cshtml:转到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();
}
答案 0 :(得分: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();
}
我希望它能奏效。