2016-08-24 28 views
2

サイトで提供されているzoom.us APIを使用しようとしています。彼らは私に新しいユーザーを作成するためにcurlコマンド与える:cURLコマンドをajaxに変換する

curl --data 'api_key=your_api_key&api_secret=your_api_secret&[email protected]&type=1&first_name=John&last_name=Smith' https://api.zoom.us/v1/user/create 

を私はAJAXに翻訳:

$.ajax({ 
     url: 'https://api.zoom.us/v1/user/create', 
     type: "POST", 
     cache: true, 
     async: false, 
     headers: { 
      'Content-Type': 'application/json' 
     }, 
     data: JSON.stringify({ 'api_key': 'key', 'api_secret': 'secret', 'email': '[email protected]', 'first_name': 'John', 'last_name': 'Smith' }), 
     success: function (res) { 
      console.log(res); 
     }, 
     error: function (err) { 
      console.error(err); 
     } 
    }); 

(注:「API_KEY」と「api_secret」の変数は、上記でちょうどプレースホルダですこのAPIコールを作成しようとするときに使用する独自のキーと秘密があります)

このコードは私にとっては機能しません。

XMLHttpRequest cannot load https://api.zoom.us/v1/user/create. 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://mywebsite.com' is therefore not allowed access. The response had HTTP status code 403. 

私の質問はこれです:私は、次の403エラーを取得する私が間違っているのでしょうか?私が誤って翻訳したものはありますか?また、私は前に同様の質問があったことを知っています(上の翻訳コードを思いついたのですが)。私は問題を解決できませんでした。

参考になる場合は、 https://support.zoom.us/hc/en-us/articles/201363033-REST-User-API

ETA:apokryfosさんのコメントの後に、ここに私の更新されたコードがあります:

XMLHttpRequest cannot load api.zoom.us/v1/user/create?api_key=key&api_secret =secret&email=test%40email.com&first_name=Juan&last_name=Gon‌​zalez. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'website.com'; is therefore not allowed access. 
+2

POSTデータとJSONデータは同じものではありません。あなたはそれを密接させることなくオブジェクトを渡すべきです。また、コンテンツタイプを変更しないでください。 – apokryfos

+3

一般的にCORS(クロスオリジンリソース共有)として知られています。私はすべてのクライアントマシンにapiの鍵/秘密を入れていると間違ったことをしていると思います。 –

+0

ありがとう@apokryfos、私の403エラーを解決しましたが、今は新しい405エラーが発生しました:XMLHttpRequestはhttps://api.zoom.us/v1/user/create?api_key=key&api_secret = secret&email = test%40email.com&first_name = Juan&last_name =ゴンザレス。要求されたリソースに「Access-Control-Allow-Origin」ヘッダーが存在しません。したがって、Origin 'http://website.com'はアクセスできません。応答にHTTPステータスコード405が付きました。 – Juan

答えて

0

@apokryfosのおかげで正解を見つけることができました。

$.ajax({ 
    url: 'https://crossorigin.me/https://api.zoom.us/v1/user/create', 
    type: "POST", 
    cache: true, 
    async: false, 
    data: { 'api_key': 'key', 'api_secret': 'secret', 'email': e, 'first_name': f, 'last_name': l }, 
    success: function (res) { 
     console.log(res); 
    }, 
    error: function (err) { 
     console.error(err); 
    } 
}); 

}

+0

また、APIプロバイダと通信して、ブラウザがCORS違反としてそのWebサービスのHTTPエラーを誤って解釈することを知らせることをお勧めします。ここではCORS違反はありません。リモートサーバーがCORS要求を受け入れていることを示すものではないため、ブラウザはそのエラーを想定しています。 – apokryfos

1

Trの:

$.ajax({ 
    url: 'https://api.zoom.us/v1/user/create', 
    cache: true, 
    async: false, 
    data: { 'api_key': 'key', 'api_secret': 'secret', 'email': e, 'first_name': 'john', 'last_name': 'smith' }, 
    success: function (res) { 
     console.log(res); 
    }, 
    error: function (err) { 
     console.error(err); 
    } 
}); 

が新たな405エラーを生成しますyを一度追加するとdataType: 'jsonp'のように

$.ajax({ 
    url: 'https://api.zoom.us/v1/user/create', 
    cache: true, 
    dataType: 'jsonp' 
    async: false, 
    data: { 'api_key': 'key', 'api_secret': 'secret', 'email': e, 'first_name': 'john', 'last_name': 'smith' }, 
    success: function (res) { 
     console.log(res); 
    }, 
    error: function (err) { 
     console.error(err); 
    } 
}); 
関連する問題