[英]Facebook API - How to get the access token?
I'm trying to get the access token following the documentation我正在尝试按照文档获取访问令牌
First, I have to redirect user to the Facebook login page.首先,我必须将用户重定向到 Facebook 登录页面。 The response looks like:
响应如下:
http://localhost/?code=AQDcY0NnkyHy2ixcmn8CR2W3F21DvXFwcyP4NgvalTIan4pCC19uInXLKNPr48FkL2VKPbY2OL98zw5XrD7lbrZ_rnT0zDs4Rumc1QOLAfD0r3Ekpac9tKmBMEImIawOm8yxmR92IL1
Fine, we can finally exchange code for an Acess Token很好,我们终于可以用代码交换 Acess Token
if(empty($_GET['code'])){
header("Location :https://www.facebook.com/v5.0/dialog/oauth?
client_id={app-id}
&redirect_uri={"h t t p s:// www.domain.com/login"}
&state={"{st=state123abc,ds=123456789});
}
else{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/v5.0/oauth/access_token?
client_id={app-id}
&redirect_uri={redirect-uri}
&client_secret={app-secret}
&code=$_GET['code']');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
}
And here I've received suprise:在这里我收到了惊喜:
string(0) ""
What is wrong?怎么了?
This what I used on one my project read comments in it.这是我在我的一个项目中使用的阅读评论。
constant
(define) can be changed to example url = "url";
constant
(定义)可以更改为示例url = "url";
//The username or email address of the account.
define('USERNAME', 'myusername');
//The password of the account.
define('PASSWORD', 'mypassword');
//Set a user agent. This basically tells the server that we are using Chrome ;)
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
//Where our cookie information will be stored (needed for authentication).
define('COOKIE_FILE', 'cookie.txt');
//URL of the login form.
define('LOGIN_FORM_URL', 'http://example.com/login.php');
//Login action URL. Sometimes, this is the same URL as the login form.
define('LOGIN_ACTION_URL', 'http://example.com/login-check.php');
//An associative array that represents the required form fields.
//You will need to change the keys / index names to match the name of the form
//fields.
$postValues = array(
'username' => USERNAME,
'password' => PASSWORD
);
//Initiate cURL.
$curl = curl_init();
//Set the URL that we want to send our POST request to. In this
//case, it's the action URL of the login form.
curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
//Tell cURL that we want to carry out a POST request.
curl_setopt($curl, CURLOPT_POST, true);
//Set our post fields / date (from the array above).
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
//We don't want any HTTPS errors.
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//Where our cookie details are saved. This is typically required
//for authentication, as the session ID is usually saved in the cookie file.
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
//Sets the user agent. Some websites will attempt to block bot user agents.
//Hence the reason I gave it a Chrome user agent.
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
//Tells cURL to return the output once the request has been executed.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//Allows us to set the referer header. In this particular case, we are
//fooling the server into thinking that we were referred by the login form.
curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
//Do we want to follow any redirects?
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
//Execute the login request.
curl_exec($curl);
//Check for errors!
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
}
//We should be logged in by now. Let's attempt to access a password protected page
curl_setopt($curl, CURLOPT_URL, 'http://example.com/protected-page.php');
//Use the same cookie file.
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
//Use the same user agent, just in case it is used by the server for session validation.
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
//We don't want any HTTPS / SSL errors.
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//Execute the GET request and print out the result.
echo curl_exec($curl);
now we come to your question
现在我们来回答你的问题
There are several ways to get access tokens, To generate an app access token, you need :有多种方法可以获取访问令牌,要生成应用访问令牌,您需要:
One of solution :解决方案之一:
curl -X GET "https://graph.facebook.com/oauth/access_token
?client_id={your-app-id}
&client_secret={your-app-secret}
&grant_type=client_credentials"
Get url by token:通过令牌获取网址:
curl -i -X GET "https://graph.facebook.com/{your-user-id}/accounts?access_token={user-access-token}
See more here on facebook documentation : https://developers.facebook.com/docs/facebook-login/access-tokens/在 facebook 文档上查看更多信息: https : //developers.facebook.com/docs/facebook-login/access-tokens/
Finall Word!最后一句话! you need to specify your
app_id
or user_id
in url as I know据我所知,您需要在 url 中指定您的
app_id
或user_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.