2

다른 서버에서 액세스 토큰을 요청할 때 약간의 어려움이 있습니다.일부 데이터를 받기 위해 httprequest를 보내십시오.

것을 얻을 요청은 다음과 같습니다

POST /auth/O2/token HTTP/1.1 
    Host: api.amazon.com 
    Content-Type: application/x-www-form-urlencoded;charset=UTF-8 

    grant_type=client_credentials&scope=messaging:push&client_id=(YOUR_CLIENT_ID)&client_secret=(YOUR_CLIENT_SECRET) 

내가 가져올 응답은 같은 것입니다 :

private String getAccessToken(String client_id,String client_secret) 
    { 

      HttpWebRequest httpWReq = 
      (HttpWebRequest)WebRequest.Create("http://api.amazon.com/auth/02/token"); 

      Encoding encoding = new UTF8Encoding(); 
      string postData = "grant_type=client_credentials"; 
      postData += "&scope=messaging:push"; 
      postData += "&client_id=" + client_id; 
      postData += "&client_secret=" + client_secret; 
      byte[] data = encoding.GetBytes(postData); 

      httpWReq.Method = "POST"; 
      httpWReq.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"; 
      httpWReq.ContentLength = data.Length; 

      using (Stream stream = httpWReq.GetRequestStream()) // ***here I get this exception : Unable to connect to the remote server !!!**** 
      { 
       stream.Write(data, 0, data.Length); 
      } 

      HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse(); 
      StreamReader reader = new StreamReader(response.GetResponseStream()); 
      String jsonresponse = ""; 
      String temp = null; 
      while ((temp = reader.ReadLine()) != null) 
      { 
       jsonresponse += temp; 
      } 

      var dict = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(jsonresponse); 
      access_token = dict["access_token"]; 
      String expires_in = dict["expires_in"]; 

     } 

     return access_token; 
    } 

I을 : 나는 그것을 통해 얻으려고 노력

X-Amzn-RequestId: d917ceac-2245-11e2-a270-0bc161cb589d 
Content-Type: application/json 

{ 
    "access_token":"Atc|MQEWYJxEnP3I1ND03ZzbY_NxQkA7Kn7Aioev_OfMRcyVQ4NxGzJMEaKJ8f0lSOiV-yW270o6fnkI", 
    "expires_in":3600, 
    "scope":"messaging:push", 
    "token_type":"Bearer" 
} 

이 실행을 받고 있는데 : , 요청시 스트림을 가져 왔을 때

+0

제목을 편집했습니다. "[제목에"태그 "가 포함되어 있어야합니까?] (http://meta.stackexchange.com/questions/19190/)"합의가 "아니오, 그렇지 않아야한다"는 것을 참조하십시오. –

답변

1

확인이 ...

이 아니 02O2 코드에서

, 그것은 오류

HttpWebRequest httpWReq = 
      (HttpWebRequest)WebRequest.Create("http://api.amazon.com/auth/**02**/token"); 

   HttpWebRequest httpWReq = 
    (HttpWebRequest)WebRequest.Create("https://api.amazon.com/auth/o2/token"); 
시도를 할 수있다

감사합니다 ...

+0

네가 맞아, 너 너무 많은 친구 야. – mouhcine

+0

@mouhcine +1 4 ur Q –

0

첫 번째 문제는 api.amazon.com/auth/02/token은 포트 80에서 수신 대기가 없음 (작동하지 않음)입니다. 그래서 아마 https가 필요합니다. (이 부분이 문서화되어있는 곳을 참고하면 더 잘 알려줄 수 있습니다)

둘째로 나는 코드의 첫 부분을 다음과 같이 대체하는 것이 더 깔끔할 것이라고 생각합니다.

using (WebClient client = new WebClient()) 
    { 
     byte[] response = client.UploadValues("https://api.amazon.com/auth/02/token", new NameValueCollection() 
     { 
      { "scope", "messaging:push" }, 
      { "client_id", "1123" }, 
      { "client_secret", "2233"} 
     }); 
     // handle response... 
    }