現在AngularでOAuthを行うアプリケーションを開発しています。
OAuthについては以下のサイトを参照しています。
https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/
内容としては、
「GETメソッドで、必要なパラメータ(client_id,scope, redirect_url, state,allow_singup)をつけて、https://github.com/login/oauth/authorize にアクセスする」
ということだと思っています。
その際、OAuth認証を行う際に投げるapiでエラーが出てしまいます。
Angular
1 2OAuthURL1: string = "https://github.com/login/oauth/authorize"; 3 4//api呼び出しメソッド内 5var httpObj = this.http.get(this.OAuthURL1, this.githubApiParam1); 6httpObj.subscribe(this.OAuth1Next, this.RequestError);
//webブラウザのコンソールより Failed to load https://github.com/login/oauth/authorize: Redirect from 'https://github.com/login/oauth/authorize' to 'https://github.com/login?return_to=%2Flogin%2Foauth%2Fauthorize' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
調べた結果、CORSという約束事でブロックされていることが分かり、Access-Control-Allow-Originの設定をする必要があることが分かりました。
その後対策を調べてみたのですが、サーバー側の.htaccessを編集するもの、リクエストヘッダーにAccess-Control-Allow-Originを追加するものなどがありました。
しかし、angularの.getメソッドには .get(URL, パラメータ)しか引数をとれず困っています。
なにか良い方法はないでしょうか。
追記
プログラムを↓のように直した結果
angular
1OAuth1() { 2 var httpObj = this.http.get(this.OAuthURL1, 3 { 4 headers: new HttpHeaders({ 5 'Content-Type': 'application/json' 6 }), 7 params: new HttpParams({ 8 client_id: this.clientId, 9 redirect_url: this.redirectUrl, 10 scope: "", // default is empty 11 state: this.state, 12 allow_singup: false // default is true 13 }) 14 }); 15 console.log(this.githubApiParam1); 16 httpObj.subscribe(this.OAuth1Next, this.RequestError); 17 }
エラー文が次のようになっています
//webブラウザより Refused to set unsafe header "Origin" Failed to load resource: the server responded with a status of 404 (Not Found) Failed to load https://github.com/login/oauth/authorize: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/11 05:58
2018/10/11 06:24
2018/10/11 06:32
2018/10/11 06:52
2018/10/11 06:57
2018/10/11 07:03
2018/10/11 07:12
2018/10/11 07:15
2018/10/11 07:23
2018/10/11 07:30
2018/10/11 07:43
2018/10/11 10:04
2018/10/11 10:31
2018/10/11 10:33