玩一把用于Google Sheets API的OAuth2认证,以获得用于Sheets API的访问令牌。
注册Google Sheets应用
首先在Google API Console注册一个应用:
Google Sheets API鉴权
- 用于用户登录的HTTPGET请求如下(scope选择了profile、对文件元数据和内容只读访问、对表单和属性只读访问):
首先要求用户登录:要求登录后用户的授权:返回页面包含授权码:GET https://accounts.google.com/o/oauth2/v2/auth? scope=https://www.googleapis.com/auth/spreadsheets.readonly https://www.googleapis.com/auth/drive.readonly profile& redirect_uri=urn:ietf:wg:oauth:2.0:oob& response_type=code& client_id=826380598768-5935tlo90sccvr691ofmp4nrvpthrnn6.apps.googleusercontent.com
- 获取访问令牌的HTTPPOST请求包含上面获得的授权码(在创建Google应用时获得的client_id和client_secret):
POST https://www.googleapis.com/oauth2/v4/token Content-Type: application/x-www-form-urlencoded code=4/-qpp...qA& client_id=826380598768-5935tlo90sccvr691ofmp4nrvpthrnn6.apps.googleusercontent.com& client_secret=5...r& redirect_uri=urn:ietf:wg:oauth:2.0:oob& grant_type=authorization_code
参考
Google Sheets
Google Sheets API
Authorize Google Sheets API Requests
Using OAuth 2.0 for Mobile and Desktop Applications
Using OAuth 2.0 for Web Server Applications