2017-10-03 8 views
0

私はNewsletter2Go APIを使いたいです。私はC#の例をダウンロードし、コンパイルされたライブラリを追加し、アクセストークンを受け取ることから始めようとしました。私はREST-APIでプログラミングするのは初めてですが、私が理解している限り、アクセストークンを受け取るために私のapi-keyと資格情報と接続する必要があります。トークンを使用して、私はさらなる操作を行うことができます。cでoauthでapiからアクセストークンを受け取る#

これはトークンを受信するためのコードです。

 //////// Configure HTTP basic authorization: Basic 
     Configuration.Default.Username = ""; 
     Configuration.Default.Password = ""; 

     var apiInstance = new AuthorizationApi(); 
     var grantType = ""; // string | grant_type (default to https://nl2go.com/jwt) 
     var username = ""; // string | username. Required for grant_type https://nl2go.com/jwt (optional) 
     var password = ""; // string | password. Required for grant_type https://nl2go.com/jwt (optional) 
     var refreshToken = ""; // string | refresh_token. Required for grant_type https://nl2go.com/jwt_refresh (optional) 

     try 
     { 
      // Endpoint for retrieving a token 
      Token result = apiInstance.GetToken(grantType, username, password, refreshToken); 
      Debug.WriteLine(result); 
     } 
     catch (Exception ex) 
     { 
      Debug.Print("Exception when calling AuthorizationApi.GetToken: " + ex.Message); 
     } 

エラーが発生します。

例外AuthorizationApi.GetToken呼び出し:入手トークンをコールする

エラー:{ "エラー": "invalid_client"、 "しましたerror_description": "クライアントの資格情報が無効である"}

Iドンapi-keyをapiに渡す方法を理解していません。ドキュメントでは、これを書いていますが、ドキュメントの中のc#にはexamplelはありません。

...and add an Authorization header with your auth key: 
xhr.setRequestHeader("Authorization", "Basic " + btoa("xhr5n6xf_Rtguwv_jzr1d3_LTshikn4_0dtesdahNvp1:Kqf2Hs#Wwazl"); 
send the request: 
xhr.send(JSON.stringify(params)); 

ここで不足していることは何ですか?前もって感謝します。

答えて

2

リクエストヘッダーにapi_keyを渡す必要があります。あなたは、あなたのアクセストークンを取得することができ、応答本体から

var client = new RestClient("https://api.newsletter2go.com/oauth/v2/token"); 
var request = new RestRequest(Method.POST); 
request.AddHeader("Authorization", "Basic xhr5n6xf_Rtguwv_jzr1d3_LTshikn4_0dtesdahNvp1:Kqf2Hs#Wwazl"); 
request.AddHeader("content-type", "application/x-www-form-urlencoded"); 
request.AddParameter("application/x-www-form-urlencoded", "grant_type=https://nl2go.com/jwt&username={username}&password={password}", ParameterType.RequestBody); 
IRestResponse response = client.Execute(request); 

:あなたはRestSharp使用して次の操作を行うことができます。

関連する問題