2017-02-06 12 views
6

これは答えではありません。私はこれをあなたと共有したいと思っていました。なぜなら、ebay OAuth 2.0とC#Webアプリケーションを組み合わせて使ってみるのはちょっと混乱していたからです。ebayからoauth 2.0経由でアクセストークンを取得するにはどうすればいいですか?#

私はRESTsharpライブラリの使用を開始しようとしましたが、本文のコンテンツが作成された時点で立ち往生しました。 RESTシャープはXMLまたはJSONを好みます、eBay wantはparamsの文字列です。

あなたが同じ問題に遭遇した場合に少しでも助けを与えるために、私は解決策を投稿することに決めました(RESTシャープを使用しない)。

public class HomeController : Controller { 
    string clientId = "YOUR_CLIENT_ID"; 
    string clientSecret = "YOUR_CLIENT_SECRET"; 
    string ruName = "YOUR_RU_NAME"; 

//ここに良いと考え方法

public ActionResult Test(string code) 
    { 
     ViewBag.Code = code; 

     // Base 64 encode client Id and client secret 
     var clientString = clientId + ":" + clientSecret; 
     byte[] clientEncode = Encoding.UTF8.GetBytes(clientString); 
     var credentials = "Basic " + System.Convert.ToBase64String(clientEncode); 

     HttpWebRequest request = WebRequest.Create("https://api.sandbox.ebay.com/identity/v1/oauth2/token") 
      as HttpWebRequest; 

     request.Method = "POST"; 
     request.ContentType = "application/x-www-form-urlencoded"; 

     request.Headers.Add(HttpRequestHeader.Authorization, credentials); 

     var codeEncoded = HttpUtility.UrlEncode(code); 

     var body = "grant_type=authorization_code&code=" + codeEncoded + "&redirect_uri=" + ruName; 

     // Encode the parameters as form data 
     byte[] formData = UTF8Encoding.UTF8.GetBytes(body); 
     request.ContentLength = formData.Length; 

     // Send the request 
     using (Stream post = request.GetRequestStream()) 
     { 
      post.Write(formData, 0, formData.Length); 
     } 

     // Pick up the response 
     string result = null; 
     using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
     { 
      StreamReader reader = new StreamReader(response.GetResponseStream()); 
      result = reader.ReadToEnd(); 
     } 

     ViewBag.Response = result; 

     return View(); 
    } 
使用し、私はコントローラに結果をテストするための方法として、テストを使用//

public ActionResult Index() { 
     var authorizationUrl = 
      "https://signin.sandbox.ebay.de/authorize?" + 
      "client_id=" + clientId + "&" + 
      "redirect_uri=" + ruName + "&" + 
      "response_type=code"; 

     Response.Redirect(authorizationUrl); 
     return View(); 
    } 

トークン要求を取得するために要求をリダイレクト

ViewBag.Responseを出力すると、認証コードが表示されます。楽しむ。

+0

ロジックをコントローラからモデルに移動します。ライブラリーで使用することができます。ライブラリーは、コンソール、Web、wpf/winformsなどの他のプロジェクトで使用できます。 – user3791372

+0

新しいドキュメント:http://developer.ebay.com/devzone/rest/sell/content/selling-ig/dev-app.htmlこれは、キーを取得する方法と完全な説明を含むより明確な内訳を提供します。 – user3791372

+0

http://developer.ebay.com/devzone/rest/ebay-rest/content/oauth-quick-ref-user-tokens.htmlにはいくつかの資料があります。私はあなたの "ActionResult Index()"スコープで見つかりませんでした。あなたはそれなしでcorectトークンを手に入れましたか? –

答えて

0

あなたのリダイレクトURLはどのようにサンドボックスで探していますか? URLのように見えるhttps。開発環境のこの段階では、httpsでサーバーを持っていません。どうやってそれに対処しましたか?

ta

+0

この点について私はブラウザの警告に問題があっても証明書を作成しました。 –

関連する問題