teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

プログラムの付け足し

2019/11/23 04:13

投稿

midorifugu2007
midorifugu2007

スコア9

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,41 @@
1
1
  #問題
2
2
  僕のサイトで、discordでログインボタンを作りたいのですが、Invalid \"redirect_uri\" in request.というエラーが出て前に進みません。
3
3
  リダイレクトurlはdiscordのDeveloperページ既に登録が済んでいます。
4
- ##インターネットでググるなどしたのですが、原因はわかりませんでした。
4
+ ##インターネットでググるなどしたのですが、原因はわかりませんでした。
5
+ プログラム
6
+ ```php
7
+ <?php
8
+ $redirect_url = 'https://bot.midokuriserver.com/webdashboard/setlogin.php';
9
+ $code = $_GET["code"];
10
+ $url = 'https://discordapp.com/api/oauth2/token';
11
+ // POST送信するデータ
12
+ $data = array(
13
+ 'client_id' => 'クライアントid',
14
+ 'client_secret' => 'クライアントシークレット',
15
+ 'grant_type' => 'authorization_code',
16
+ 'code' => $code,
17
+ 'redirect_url' => $redirect_url,
18
+ 'scope' => 'identify'
19
+ );
20
+
21
+ // URL エンコード
22
+ $data = http_build_query($data, "", "&");
23
+
24
+ // 送信時のオプション
25
+ $options = array('http' => array(
26
+ 'method' => 'POST',
27
+ 'content' => $data,
28
+ 'protocol_version' => '1.1',
29
+ 'ignore_errors' => true
30
+ ));
31
+
32
+ // ストリームコンテキストを作成
33
+ $options = stream_context_create($options);
34
+
35
+ // file_get_contents
36
+ $contents = file_get_contents($url, false, $options);
37
+
38
+ // 出力
39
+ echo $contents;
40
+ ?>
41
+ ```