2

他のサーバーからアクセス トークンを要求する際に問題があります。

それを取得するためのリクエストは次のとおりです。

  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)

取得したい応答は次のようなものです。

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"
 }

私はそれをやり遂げようとしました:

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;
    }

私はこのexecptionを取得しています: Unable to connect to the remote server、リクエストストリームを取得しようとしたとき

4

2 に答える 2

1

これをチェックして ...

02それはそうではないO2

あなたのコードでは、それはエラーかもしれません

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

これを試して

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

ありがとうございました ...

于 2013-07-23T13:31:26.020 に答える