2017-02-04 20 views
2

Oxford Dictionaries APIに接続しようとしています。私はIDをダブルチェックHttpWebRequest資格情報をOxford APIに渡す

 HttpWebRequest req = null; 
     string PrimeUrl = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/" 
     string uri = PrimeUrl+word ; 
     req = (HttpWebRequest)HttpWebRequest.Create(uri); 
     string credentials = String.Format("{0}:{1}", uid, pwd); 
     byte[] bytes = Encoding.ASCII.GetBytes(credentials); 
     string base64 = Convert.ToBase64String(bytes); 
     string authorization = String.Concat("Basic ", base64); 
     req.Headers.Add("Authorization", authorization); 
     req.Method = WebRequestMethods.Http.Get; 
     req.Accept = "application/json";    
     HttpWebResponse HWR_Response = (HttpWebResponse)req.GetResponse(); 

import requests 
import json 
app_id = 'my id' 
app_key = 'my key' 
language = 'en' 
word_id = 'Ace' 
url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language  + '/' + word_id.lower() 
r = requests.get(url, headers = {'app_id': app_id, 'app_key': app_key}) 
print("code {}\n".format(r.status_code)) 

が、C#で、私は次のコードを使用して403認証が失敗したエラーを取得していたときに:私はこのコードでのpythonを使用していたとき、私は問題のありませんキーが正しいかどうか、すべて試してみたhere

私の最後の手段です。ありがとうございました。

+0

コード? – Crowcoder

+0

私は、基本的なpythonのデフォルトと仮定します。 –

+0

私はそれを疑っていますが、そうであってもあなたのユーザ名とパスワードを推測することはできません。あなたの要求をPostmanで行い、そのコードジェネレータを使ってC#を作成します。 RestSharpへの参照を持参する必要があります。 – Crowcoder

答えて

3

これは正しくリクエストにあなたの開発者の資格情報を追加する方法である - 彼らは、ヘッダーです:

using System; 
using System.Diagnostics; 
using System.IO; 
using System.Net; 
using System.Text; 

namespace OxfDictionary 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      HttpWebRequest req = null; 

      string PrimeUrl = "https://od-api.oxforddictionaries.com:443/api/v1/entries/en/"; 
      string uri = PrimeUrl + "robot"; 
      req = (HttpWebRequest)HttpWebRequest.Create(uri); 

      //These are not network credentials, just custom headers 
      req.Headers.Add("app_id", "5a......3"); 
      req.Headers.Add("app_key", "d12............1a0"); 

      req.Method = WebRequestMethods.Http.Get; 
      req.Accept = "application/json"; 

      using (HttpWebResponse HWR_Response = (HttpWebResponse)req.GetResponse()) 
      using (Stream respStream = HWR_Response.GetResponseStream()) 
      using (StreamReader sr = new StreamReader(respStream, Encoding.UTF8)) 
      { 
       string theJson = sr.ReadToEnd(); 
       Debug.WriteLine(theJson); 
       Console.WriteLine(theJson); 
      } 
      Console.ReadKey(); 
     } 
    } 
} 

そして結果は次のとおりです。

C#のではなく、pythonで基本認証を使用する理由
{ 
     "metadata": { 
      "provider": "Oxford University Press" 
     }, 
     "results": [ 
      { 
       "id": "robot", 
       "language": "en", 
       "lexicalEntries": [ 
        { 
         "entries": [ 
          { 
           "etymologies": [ 
            "from Czech, from robota forced labour. The term was coined in K. Čapek's play R.U.R. Rossum's Universal Robots (1920)" 
           ], 
           "grammaticalFeatures": [ 
            { 
             "text": "Singular", 
             "type": "Number" 
            } 
           ], 
           "senses": [ 
            { 
             "definitions": [ 
              "a machine capable of carrying out a complex series of actions automatically, especially one programmable by a computer:" 
             ], 
             "domains": [ 
              "Electronics" 
             ], 
             "examples": [ 
              { 
               "text": "a robot arm" 
              }, 
              { 
               "text": "half of all American robots are making cars or trucks" 
              } 
             ], 
             "id": "m_en_gb0714510.001", 
             "subsenses": [ 
              { 
               "definitions": [ 
                "(especially in science fiction) a machine resembling a human being and able to replicate certain human movements and functions automatically:" 
               ], 
               "examples": [ 
                { 
                 "text": "the robot closed the door behind us" 
                } 
               ], 
               "id": "m_en_gb0714510.002" 
              }, 
              { 
               "definitions": [ 
                "a person who behaves in a mechanical or unemotional manner:" 
               ], 
               "examples": [ 
                { 
                 "text": "public servants are not expected to be mindless robots" 
                } 
               ], 
               "id": "m_en_gb0714510.003" 
              } 
             ] 
            }, 
            { 
             "crossReferenceMarkers": [ 
              "another term for crawler (in the computing sense)" 
             ], 
             "crossReferences": [ 
              { 
               "id": "crawler", 
               "text": "crawler", 
               "type": "see also" 
              } 
             ], 
             "domains": [ 
              "Computing" 
             ], 
             "id": "m_en_gb0714510.004" 
            }, 
            { 
             "definitions": [ 
              "a set of automatic traffic lights:" 
             ], 
             "domains": [ 
              "Motoring" 
             ], 
             "examples": [ 
              { 
               "text": "waiting at a robot I caught the eye of a young woman" 
              } 
             ], 
             "id": "m_en_gb0714510.005", 
             "regions": [ 
              "South African" 
             ] 
            } 
           ] 
          } 
         ], 
         "language": "en", 
         "lexicalCategory": "Noun", 
         "pronunciations": [ 
          { 
           "audioFile": "http://audio.oxforddictionaries.com/en/mp3/robot_gb_1.mp3", 
           "dialects": [ 
            "British English" 
           ], 
           "phoneticNotation": "IPA", 
           "phoneticSpelling": "ˈrəʊbɒt" 
          } 
         ], 
         "text": "robot" 
        } 
       ], 
       "type": "headword", 
       "word": "robot" 
      } 
     ] 
    } 
+0

Crowcoderに感謝します。あなたは私の日を救う。 –

関連する問題