我想从Google获取访问令牌. Google API说要获取访问令牌,请将代码和其他参数发送到令牌生成页面,响应将是一个JSON对象,如:
{
"access_token" : "ya29.AHES6ZTtm7SuokEB-RGtbBty9IIlNiP9-eNMMQKtXdMP3sfjL1Fc",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "1/HKSmLFXzqP0leUihZp2xUt3-5wkU7Gmu2Os_eBnzw74"
}
Run Code Online (Sandbox Code Playgroud)
但是,我没有收到刷新令牌.我的回答是:
{
"access_token" : "ya29.sddsdsdsdsds_h9v_nF0IR7XcwDK8XFB2EbvtxmgvB-4oZ8oU",
"token_type" : "Bearer",
"expires_in" : 3600
}
Run Code Online (Sandbox Code Playgroud) 问题:缺少OAuth 2刷新令牌.
问题是localhost版本接收Refresh Token授权令牌的一部分,但GCE中运行的相同代码不会.
细节:
我编写了一个实现Google OAuth 2.0的Python Flask应用程序.此Web应用程序在云中运行,具有经过验证的域名,有效的SSL证书和HTTPS端点.此未修改的Web应用程序也以localhost.运行时之间的区别在于localhost版本不使用TLS.代码流中没有其他差异.
除了Refresh Token缺少,我不能自动更新token,一切都很完美.
我已经广泛研究过这个问题.access_type=offline正确实现API等问题,否则我不会Refresh Token在localhost版本中获得.
我正在使用requests_oauthlibpython库.
gcp = OAuth2Session(
app.config['gcp_client_id'],
scope=scope,
redirect_uri=redirect_uri)
# print('Requesting authorization url:', authorization_base_url)
authorization_url, state = gcp.authorization_url(
authorization_base_url,
access_type="offline",
prompt="select_account",
include_granted_scopes='true')
session['oauth_state'] = state
return redirect(authorization_url)
# Next section of code after the browser approves the request
token = gcp.fetch_token(
token_url,
client_secret=app.config['gcp_client_secret'],
authorization_response=request.url)
Run Code Online (Sandbox Code Playgroud)
令牌refresh_token在运行时有,localhost但在云中运行时没有.
此Google文档讨论了刷新令牌,表明Web应用程序支持此功能.
[更新11/18/2018]
我发现这个 …
google-openid oauth-2.0 google-oauth google-cloud-platform requests-oauthlib
我正在尝试使用基于Google文档的Google YouTube数据API和PHP:https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Refreshing_a_Token.在使用OAuth进行身份验证时,我的问题就出现了.我正在使用以下授权URL,该URL与文档所说的相同,除了我的重定向uri和应用程序密钥,显然.
$this->authorizationUrl = 'https://accounts.google.com/o/oauth2/auth?';
$this->authorizationUrl .= 'client_id=' . $this->applicationKey . '&';
$this->authorizationUrl .= 'redirect_uri=' . $this->redirect_uri . '/account.php?action=youtube_oauth&';
$this->authorizationUrl .= 'scope=https://gdata.youtube.com&';
$this->authorizationUrl .= 'response_type=code&';
$this->authorizationUrl .= 'access_type=offline';
Run Code Online (Sandbox Code Playgroud)
然后,正如文档所说,我提出以下内容:
$curl_options = Array(
CURLOPT_POSTFIELDS => Array(
'code' => $code,
'client_id' => $this->applicationKey,
'client_secret' => $this->applicationSecret,
'redirect_uri' => $this->redirect_uri . '/account.php?action=youtube_oauth',
'grant_type' => 'authorization_code'
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'https://accounts.google.com/o/oauth2/token'
);
Run Code Online (Sandbox Code Playgroud)
但是,我的回复从来没有像我们的文档所说的那样给我一个refresh_token.我只得到其他三个响应项.
像这样的一些问题:获取刷新令牌google api已经说过使用approval_prompt = force,但这也不起作用,完全违背了access_type = offline的目的.
有关为什么我会得到4个响应项中的3个的有效回复的任何想法?
用户在我的Android应用程序中授权.我正在将用户的令牌和其他信息发送到我的服务器.在这个服务器上,我想为用户实现一些逻辑.
我希望得到这个流程.
我按照此链接中的步骤quickstart.php来获取用户在服务器上的日历.
但我得到以下错误:
google oauth exception'with message'无法json解码令牌'
因此我尝试了这个解决方案.但我也犯同样的错误.因此,作为第三个选项我创建的JSON格式自己像这个解决方案如下.
$access_token = '{
"access_token":'.$access_token.',
"token_type":"Bearer",
"expires_in":3600,
"id_token":'.$id_token.',
"refresh_token":" ",
"created":'. time() .'
}';
Run Code Online (Sandbox Code Playgroud)
如你所见,我不知道如何交换刷新令牌.我搜索了如何获得刷新令牌并看到了这个问题.并将此解决方案应用于我的代码但没有任何改变.
编辑4:我试图在Android应用程序中根据此答案获取访问令牌并将其发送到应用服务器.我像以前一样接受代码:
GoogleSignInAccount acct = result.getSignInAccount();
code = acct.getServerAuthCode();//sending this code to AsyncTask to get access token
Run Code Online (Sandbox Code Playgroud)
我获取访问令牌的功能:
private void getAccessToken()throws GoogleAuthException, IOException{
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
List<String> scopes = new LinkedList<String>();
scopes.add("https://www.googleapis.com/auth/calendar");
scopes.add("https://www.googleapis.com/auth/calendar.readonly"); …Run Code Online (Sandbox Code Playgroud) 我使用用户的以下范围获得oauth2访问令牌.
scope: ["https://www.googleapis.com/auth/youtube"]
Run Code Online (Sandbox Code Playgroud)
然后我继续使用此令牌来调用youtube v3 api.这些调用最初工作,但后来我开始得到这个响应/错误.
{ errors: [
{
domain: 'global',
reason: 'authError',
message: 'Invalid Credentials',
locationType: 'header',
location: 'Authorization'
}],
code: 401,
message: 'Invalid Credentials'
}
Run Code Online (Sandbox Code Playgroud)
还有其他人遇到这种间歇性错误吗?有没有办法像facebook 工具一样调试oauth令牌?令牌似乎在1小时后到期.获取刷新令牌google api
示例网址
https://www.googleapis.com/youtube/v3/channels/?part=id,snippet,contentDetails,statistics,topicDetails&mine=true&access_token= {access_token}
我有以下测试程序:
from rauth.service import OAuth1Service, OAuth2Service
SUPPORTED_SERVICES = {
'twitter' : ( 'OAuth1', 'twitter', 'https://api.twitter.com/oauth', 'request_token', 'access_token', 'authorize', 'https://api.twitter.com/1/', None),
'facebook' : ( 'OAuth2', 'facebook', 'https://graph.facebook.com/oauth', None, 'access_token', 'authorize', 'https://graph.facebook.com/', 'https://www.facebook.com/connect/login_success.html'),
'google' : ( 'OAuth2', 'google', 'https://accounts.google.com/o/oauth2', None, 'token', 'auth', None, 'http://localhost'),
}
CLIENT_DATA = {
'twitter' : ('dummy_client_id', 'dummy_client_secret'),
'facebook' : ('dummy_client_id', 'dummy_client_secret'),
'google' : ('dummy_client_id', 'dummy_client_secret'),
}
USER_TOKENS = {
'user1' : {
'twitter' : ('dummy_access_token', 'dummy_access_token_secret'),
'facebook' : ('dummy_access_token', None),
'google' : ('dummy_access_token', None),
}
}
def …Run Code Online (Sandbox Code Playgroud) oauth-2.0 ×3
google-api ×2
google-oauth ×2
oauth ×2
php ×2
youtube-api ×2
access-token ×1
android ×1
gdata ×1
gdata-api ×1
python ×1
rauth ×1
youtube ×1