我很难理解作用域的工作原理。
我找到了 here描述 stackexchange api 范围的小文本但我需要更多关于它们如何工作的信息(不是特别是这个......)。有人可以给我一个概念吗?
提前致谢
最佳答案
要授权应用程序,您需要为 OAuth2 授权过程调用 URL。该 URL 在 API 的提供者文档中“有效”。例如谷歌有这个 url:
https://accounts.google.com/o/auth2/auth
您还需要使用此链接指定一些查询参数:
client_id
redirect_uri
scope
:您的应用程序请求访问的数据。这通常指定为以空格分隔的字符串列表,尽管 Facebook 使用逗号分隔的字符串。 API 提供程序文档中应包含scope
的有效值。对于 Gougle Tasks,scope
是https://www.googleapis.com/auth/tasks
。如果应用程序还需要访问 Google 文档,它将指定scope
值为https://www.googleapis.com/auth/tasks
https://docs.google.com/feeds
response_type
:服务端web应用流程的code
,表示在用户访问后返回一个授权code
给应用批准授权请求。state
:您的应用程序使用的唯一值,以防止对您的实现进行跨站点请求伪造 (CSRF) 攻击。该值应该是针对此特定请求的随机唯一字符串,不可猜测并在客户端保密(可能在服务器端 session 中)
// Generate random value for use as the 'state'. Mitigates
// risk of CSRF attacks when this value is verified against the
// value returned from the OAuth provider with the authorization
// code.
$_SESSION['state'] = rand(0,999999999);
$authorizationUrlBase = 'https://accounts.google.com/o/oauth2/auth';
$redirectUriPath = '/oauth2callback.php';
// For example only. A valid value for client_id needs to be obtained
// for your environment from the Google APIs Console at
// http://code.google.com/apis/console.
$queryParams = array(
'client_id' => '240195362.apps.googleusercontent.com',
'redirect_uri' => (isset($_SERVER['HTTPS'])?'https://':'http://') .
$_SERVER['HTTP_HOST'] . $redirectUriPath,
'scope' => 'https://www.googleapis.com/auth/tasks',
'response_type' => 'code',
'state' => $_SESSION['state'],
'approval_prompt' => 'force', // always request user consent
'access_type' => 'offline' // obtain a refresh token
);
$goToUrl = $authorizationUrlBase . '?' . http_build_query($queryParams);
// Output a webpage directing users to the $goToUrl after
// they click a "Let's Go" button
include 'access_request_template.php';
Google Authorization Server 支持的用于网络服务器应用程序的查询字符串参数集位于此处:
https://developers.google.com/accounts/docs/OAuth2WebServer?hl=el#formingtheurl
关于database - OAuth2 服务器的范围值是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15744319/