python版本:2.7
在OAuth2.0授权中, 已获得授权链接, 如何通过python 实现获取其授权码auth_code?
正常情况下应该是: 用户点击授权链接,在登录的状态下(如果没有,会跳转到登录页进行登录)会跳转到统一授权页,用户点击授权后,回调开发者提供的callback URL,返回一个临时授权码auth_code.
如何通过python代码实现上面的用户点击授权步骤, 而不需要打开浏览器输入链接点击授权, 再粘贴返回的auth_code, 直接在python脚本里获取返回的URL和临时授权码auth_code呢?
相关文档参考: https://dev2.baidu.com/content?sceneType=0&pageId=100441&nodeId=421&subhead=
你如果不需要用户参与,那就用客户端模式,而不是授权码模式,正常情况下,你不会有用户的账户信息的
def main():
# get accessToken
auth_code = "{authCode}"
secret_key = "{secretKey}"
app_id = "{appId}"
user_id = 0 # {userId}
grant_type = "access_token"
oauth_service = OAuthService()
request = GetAccessTokenRequest(app_id,
auth_code,
secret_key,
grant_type,
user_id)
response = oauth_service.get_access_token(request)
access_token = response.data.accessToken
# init service
account_service = AccountService()
# init request header
request_header = ApiRequestHeader(
userName="{userName}",
accessToken=access_token,
_spec_property_naming=True
)
# init request
account_query_request = ApiAccountQueryRequest(
accountFields=["pictureOptimizeSegmentStatus", "balance", "userLevel"],
_spec_property_naming=True
)
# init request wrapper
request_wrapper = GetAccountInfoRequestWrapper(
header=request_header,
body=account_query_request,
_spec_property_naming=True
)
response_wrapper = account_service.get_account_info(request_wrapper)
print(response_wrapper)
if __name__ == '__main__':
main()