質問編集履歴

1

下書きと間違えて投稿してしまったため、全文を入れました

2020/01/31 14:31

投稿

kmtym1998
kmtym1998

スコア9

test CHANGED
@@ -1 +1 @@
1
- PHPでGoogleのOAuth2.0
1
+ PHPでGoogleのOAuth2.0、file_get_contets()が使えない
test CHANGED
@@ -13,6 +13,8 @@
13
13
 
14
14
 
15
15
  ```php
16
+
17
+ //index.php
16
18
 
17
19
  <?php
18
20
 
@@ -51,3 +53,121 @@
51
53
  ```
52
54
 
53
55
  リダイレクトURIに指定したページが以下のPHPファイルです。
56
+
57
+
58
+
59
+ ```php
60
+
61
+ //display.php
62
+
63
+ <?php
64
+
65
+ // アプリケーション設定
66
+
67
+ define('CONSUMER_KEY', '<クライアントID>');
68
+
69
+ define('CONSUMER_SECRET', '<クライアント シークレット>');
70
+
71
+ define('CALLBACK_URL', 'http://localhost/test/oauth.php');
72
+
73
+ // URL
74
+
75
+ define('TOKEN_URL', 'https://accounts.google.com/o/oauth2/token');
76
+
77
+ define('INFO_URL', 'https://www.googleapis.com/oauth2/v1/userinfo');
78
+
79
+ $params = array(
80
+
81
+ 'code' => $_GET['code'],
82
+
83
+ 'grant_type' => 'authorization_code',
84
+
85
+ 'redirect_uri' => CALLBACK_URL,
86
+
87
+ 'client_id' => CONSUMER_KEY,
88
+
89
+ 'client_secret' => CONSUMER_SECRET,
90
+
91
+ );
92
+
93
+ // POST送信
94
+
95
+ $options = array('http' => array(
96
+
97
+ 'method' => 'POST',
98
+
99
+ 'content' => http_build_query($params)
100
+
101
+ ));
102
+
103
+ // アクセストークンの取得
104
+
105
+ $res = file_get_contents(TOKEN_URL, false, stream_context_create($options));
106
+
107
+ // レスポンス取得
108
+
109
+ $token = json_decode($res, true);
110
+
111
+ if(isset($token['error'])){
112
+
113
+ echo 'エラー発生';
114
+
115
+ exit;
116
+
117
+ }
118
+
119
+ $access_token = $token['access_token'];
120
+
121
+ $params = array('access_token' => $access_token);
122
+
123
+ // ユーザー情報取得
124
+
125
+ $res = file_get_contents(INFO_URL . '?' . http_build_query($params));
126
+
127
+ //表示
128
+
129
+ echo $res;
130
+
131
+ ?>
132
+
133
+ ```
134
+
135
+
136
+
137
+ # エラー
138
+
139
+ ```
140
+
141
+
142
+
143
+ Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ~display.php on line 22
144
+
145
+
146
+
147
+ Warning: file_get_contents(https://accounts.google.com/o/oauth2/token): failed to open stream: no suitable wrapper could be found in ~display.php on line 22
148
+
149
+
150
+
151
+ Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 in ~display.php on line 32
152
+
153
+
154
+
155
+ Warning: file_get_contents(https://www.googleapis.com/oauth2/v1/userinfo?): failed to open stream: no suitable wrapper could be found in ~display.php on line 32
156
+
157
+ ```
158
+
159
+ `index.php`にアクセスし、`display.php`にリダイレクトした際にこちらのエラーが出力されます。
160
+
161
+ 軽く調べてみたとろ、`php.ini`の`allow_url_include`に問題があるのかな?と思いましたが、自分が使用しているXFREEサーバでは、そこをいじることができません。
162
+
163
+
164
+
165
+ いろいろと調べてみましたが、いまいち原因がわからず手詰まってしまいました。
166
+
167
+ - file_get_contents()が警告なしで使える方法は他にあるか
168
+
169
+ - 以上で試している方法以外で、Googleアカウントのプロフィール情報を取得できる方法はあるか
170
+
171
+
172
+
173
+ こちらのいずれか、または両方についてご回答いただければ幸いです。