Li

Delete

Are you sure you want to delete this?

PaypalModel


Date
20211028
Target
C_005
Title
[성공] Get an access token in C# / paypal get access token c#
Contents
[1] 검색어 paypal access token in C#로 구글링해 아래 주소를 얻고 https://stackoverflow.com/questions/48838108/c-sharp-paypal-acess-token-does-not-return-anything-although-connection-is-ok 코드 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.sandbox.paypal.com/v1/oauth2/token"); request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(clientId + ":" + clientSecret)); request.Accept = "application/json"; request.Headers.Add("Accept-Language", "en_US"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Timeout = 10000; byte[] postBytes = Encoding.ASCII.GetBytes("grant_type=client_credentials"); Stream postStream = request.GetRequestStream(); postStream.Write(postBytes, 0, postBytes.Length); postStream.Flush(); postStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 에서 request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(clientId + ":" + clientSecret)); 를 힌트로 얻어 [2] https://developer.paypal.com/docs/business/get-started/ Get API credentials for an access token cURL방법에서 curl -v POST https://api-m.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "CLIENT_ID:SECRET" \ -d "grant_type=client_credentials" 를 [3] Converting - Convert Curl to Http Request at the site [https://reqbin.com/req/csharp/c-w7oitglz/convert-curl-to-http-request] var url = "https://api-m.sandbox.paypal.com/v1/oauth2/token"; var httpRequest = (HttpWebRequest)WebRequest.Create(url); httpRequest.Method = "POST"; httpRequest.Accept = "application/json"; httpRequest.Headers["Accept-Language"] = "en_US"; httpRequest.ContentType = "application/x-www-form-urlencoded"; httpRequest.Headers["Authorization"] = "Basic Q0xJRU5UX0lEOlNFQ1JFVA=="; var data = "grant_type=client_credentials"; using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream())) { streamWriter.Write(data); } var httpResponse = (HttpWebResponse)httpRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); } Console.WriteLine(httpResponse.StatusCode); 를 얻고 [1]에서 얻은 힌트를 Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(clientId + ":" + clientSecret));를 [Converting] 한 httpRequest.Headers["Authorization"] = "Basic Q0xJRU5UX0lEOlNFQ1JFVA=="; 에 httpRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(clientId + ":" + clientSecret)); 와 같이 적용한 후 이것을 코딩한 페이지 paypalGetAccessToken, https://www.tanz.co.kr/PaymentMethod/paypal/paypalGetAccessToken.aspx 를 실행하고 [4] 응답 아래 {"scope":"https://api.paypal.com/v1/payments/.* https://uri.paypal.com/services/invoicing openid https://api.paypal.com/v1/vault/credit-card/.* https://uri.paypal.com/services/subscriptions https://uri.paypal.com/services/applications/webhooks https://api.paypal.com/v1/vault/credit-card","access_token":"A21AALEjJfzsP2I_4bDmfnEPDvGUIiGr2RUtyZualjU3goTnMODZN318Fu-nTqCA52Y-2tDl4h66Clr9M4jAjECkYMHTA1-9A","token_type":"Bearer","app_id":"APP-80W284485P519543T","expires_in":31506,"nonce":"2021-10-29T01:04:20Zb0S6HInaHpplfDEeBbH-fWUFmR3T5pDfqopOUPaoJ7o"}OK 와 같은 결과값을 얻었다. 성공이다. [이전의 과정들] Access Token을 얻는다는 것이 무언지, 그리고 Access Token 이전에 [1]PayPal에 비지니스 계정으로 가입해 내 쇼핑몰에 페이발 구매하기 버튼을 달려면 사전에 [2]Get API credentials해야하고 그 후 이 [3] credentials로 Exchange your API credentials for an access token 하고 그 다음에 드디어 [4] access payments and make payments를 하게 된다는 전체 과정에 대한 개요를 감잡았다. [1]과정에서 얻은 credentials이다. CLIENT_ID : Aaa6bBAPUZTOPAWLJy1E28dcVXA6a7v-MfajSO_o8v-JBZV76v67wjzE2-rB SECRET : EB4OYhCD2-TreOX8_oWtk2aJsgySeiXlln_Mevmm1NikSbQg5JtwgaT6p7HP [2]과정 Get an access token POSTMAN과 cURL 두 가지 다 해 보았다. 그 결과 Access Token을 확인할 수 있었다. Your access token authorizes you to use the PayPal REST API server. To call a REST API in your integration, exchange your client ID and secret for an access token in an OAuth 2.0 token call. While there are a few ways to get a token, here are examples using both the Postman app and a cURL command. In the Postman app, complete the following: Set the verb to POST. Enter https://api-m.sandbox.paypal.com/v1/oauth2/token as the request URL. Select the Authorization tab. From the TYPE list, select Basic Auth. In the Username field, enter your client ID. In the Password field, enter your secret. Select the Body tab. Select the x-www-form-urlencoded option. In the KEY field, enter grant_type. In the VALUE field, enter client_credentials. Select Send. cURL Copy the following code and modify it. curl -v POST https://api-m.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "CLIENT_ID:SECRET" \ -d "grant_type=client_credentials" curl -v POST https://api-m.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: application/json" \ -H "Accept-Language: en_US" \ -u "Aaa6bBAPUZTOPAWLJy1E28dcVXA6a7v-MfajSO_o8v-JBZV76v67wjzE2-rB:EB4OYhCD2-TreOX8_oWtk2aJsgySeiXlln_Mevmm1NikSbQg5JtwgaT6p7HP" \ -d "grant_type=client_credentials" Change CLIENT_ID to your client ID. Change SECRET to your secret. [?]이건언제사용하는건지모르겠다. access_token$sandbox$yppkcfhk4cbcjt2m$0ed54ec481244716c0db469f70b3e601 아래는 또 다른 앱의 credential(CLIENT_ID, SECRET) A21AALxfHGstIcMrmL7uHh4TdubPt_cpJIhtfhrkYYDrHJTl-VlL4KSStkYhWe0jDREu6Yd6hkiSZVRuJ5tR2QFN7pFy5J_nQ A21AAJRMWHPvnlwONNDOOKjZHWPmB9N7qdih2MqdDZvwKuM3ZAuYKPlm5UT6bYVCh1UEt_dc81MbO8GfAeDw0ahTdF_Pev_Hg Make REST API calls In REST API calls, include the URL to the API service for the environment: Sandbox: https://api-m.sandbox.paypal.com Live: https://api-m.paypal.com [3] Access Token 얻은 값 생략 ( 위 [4] 참고) [4] access payments and make payments 하는 코드다 Bearer 뒤에 붙은 것이 ACCESS TOKEN이다 [1], [2] 과정으로 얻은 curl -v -X POST https://api-m.sandbox.paypal.com/v2/checkout/orders \ -H "Content-Type: application/json" \ -H "Authorization: Bearer A21AALxfHGstIcMrmL7uHh4TdubPt_cpJIhtfhrkYYDrHJTl-VlL4KSStkYhWe0jDREu6Yd6hkiSZVRuJ5tR2QFN7pFy5J_nQ" \ -d '{ "intent": "CAPTURE", "purchase_units": [ { "amount": { "currency_code": "USD", "value": "100.00" } } ] }'