English 中文(简体)
打开流失败: HTTP 请求失败! HTTP/ 1.0 400 坏请求 - OAuth 2. 0 POST
原标题:Failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request - OAuth 2.0 POST
I am working with YouTube APIs for my college project, and I keep getting an error. Here I send them to the authorisation page to log in, when they allow access it sends the $_GET[ code ] string back. Then I send this along with some other data and it should send back a JSON object. Instead I am just getting Warning: file_get_contents(https://accounts.google.com/o/oauth2/token) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in http://www.example.net/callback.php on line 27 I have replaced my domain with example.net just for security urlencode($_GET[ code ]), client_id => urlencode( 111522767640.apps.googleusercontent.com ), client_secret => urlencode( secret ), redirect_uri => urlencode( http://example.net/callback.php ), grant_type => urlencode( authorization_code ) ) ); $params = array( http => array( method => POST /o/oauth2/token HTTP/1.1 , header => Host: accounts.google.com . Content-Type: application/x-www-form-urlencoded , content => $postdata ) ); $context = stream_context_create($params); $result = file_get_contents( https://accounts.google.com/o/oauth2/token , false,$context); var_dump($_SESSION); var_dump($result); } else //If code isnt set, user must have come here erroniously or has denied access to this program { //header( Location: www.example.net/error.php ) ; } ?>
问题回答
file_get_contents is going to make a GET request to the url specified, but oauth2/token needs a POST request. See reference Google OAuth2, PHP HTTP.
if you are using oauth2, goto libraries/oauth2/provider.php and uncomment the code line 182 shows $ci = get_instance(); $ci->load->spark( curl/1.2.1 ); $ci->curl ->create($url) ->post($params, array( failonerror => false)); $response = $ci->curl->execute();
Here s how to do the Google oAuth properly: $config = (object) array( CLIENT_ID => AAAAA , CLIENT_SECRET => BBBBB , REFRESH_TOKEN => CCCCC , ); $sJSON = file_get_contents( https://www.googleapis.com/oauth2/v4/token ,FALSE, stream_context_create(array( http =>array( ignore_errors => TRUE, // see errors in response instead of empty on error method => POST , header => array( Content-Type: application/x-www-form-urlencoded ), content => http_build_query(array( grant_type => refresh_token , client_id => $config->CLIENT_ID, client_secret => $config->CLIENT_SECRET, refresh_token => $config->REFRESH_TOKEN )) ))) ); You can then use json_decode() to parse $sJSON into an object and then retrieve the access_token property. For those who are wondering how to get the CLIENT_ID, CLIENT_SECRET, and REFRESH_TOKEN, watch this video. It s not easy. In my case, I needed to do this for Google Adwords API. So, I had to get my Developer Token and Login Customer ID from https://ads.google.com/home/tools/manager-accounts. Then, I had to go to https://console.developers.google.com/apis/credentials with this guide to generate a Client ID and Client Secret. Then, I followed this guide to learn how to get my Refresh Token.




相关问题
How should I use https in Ruby on Rails

I m developing an application in Rails (2.3.4) and there are some parts that need to run over a secure protocol, for example, the login form, the credit card form, etc. How can I do just these pages ...

How to open a stream to httpS URL

I want to open a stream to a httpS URL and read the data. Kindly let me know how to do it. Regards Chaitanya

direct http to https on certain pages?

Hi I have added the below code to .ht access but how can I add another page to this? such as login.php also if the user types in www. they get a "untrusted connection" as the SSL is only valid ...

See what content is not sent over HTTPS

I created a page that is HTTPS only. On my browsers, I always get a warning that the page includes resources that are not secured. I just can t find out why! Looking at the source code seems fine. All ...

C# maintaining session over HTTPS on the client

I need to login to a website and perform an action. The website is REST based so I can easily login by doing this (the login info is included as a querystring on the URL, so I dont t need to set the ...

热门标签