我正在使用javascript从用户那里获取id令牌,并且想要离线访问以及访问他的gmail信息。问题是弹出窗口只显示gmail和基本信息授权。离线授权弹出窗口仅在再次单击该按钮后才会显示。在那之后点击按钮将继续给出离线弹出窗口。为什么我不能把它们都放在一个窗口里呢?或者,我如何至少让它们同时弹出?
这是我的代码:
function start() {
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: ' ',
}).then(function () {
auth2 = gapi.auth2.getAuthInstance();
if (auth2.isSignedIn.get()) {
window.location.href = "ClientHome.aspx";
}
});
});
}
function LogIn() {
auth2.grantOfflineAccess({ redirect_uri: 'postmessage', approval_prompt: 'force', scope: 'https://mail.google.com/', include_granted_scopes: 'true' }).then(signInCallback);发布于 2016-04-19 18:20:58
对Gmail API的请求必须使用OAuth2.0 credentials Offline access进行授权。当您的应用程序需要代表用户访问Google API时,您应该使用服务器端流。这种方法需要将一次性授权码从客户端传递到服务器。它将为您服务器获取访问令牌和刷新令牌。访问令牌被传递给Gmail API,以授予您的应用程序访问用户数据的权限。
下面的代码示例演示如何使用脱机访问交换访问令牌的授权码并存储刷新令牌。
def get_authorization_url(email_address, state):
"""Retrieve the authorization URL.
Args:
email_address: User's e-mail address.
state: State for the authorization URL.
Returns:
Authorization URL to redirect the user to.
"""
flow = flow_from_clientsecrets(CLIENTSECRETS_LOCATION, ' '.join(SCOPES))
flow.params['access_type'] = 'offline'
flow.params['approval_prompt'] = 'force'
flow.params['user_id'] = email_address
flow.params['state'] = state
return flow.step1_get_authorize_url(REDIRECT_URI)
def get_credentials(authorization_code, state):
"""Retrieve credentials using the provided authorization code.
email_address = ''
try:
credentials = exchange_code(authorization_code)
user_info = get_user_info(credentials)
email_address = user_info.get('email')
user_id = user_info.get('id')
if credentials.refresh_token is not None:
store_credentials(user_id, credentials)
return credentials
else:
credentials = get_stored_credentials(user_id)
if credentials and credentials.refresh_token is not None:
return credentials
except CodeExchangeException, error:
logging.error('An error occurred during code exchange.')
# Drive apps should try to retrieve the user and credentials for the current
# seshttps://stackoverflow.com/questions/36683982
复制相似问题