请求Office 365管理API的访问令牌时出现SSL错误

时间:2019-01-02 06:56:17

标签: python-requests office365

我已经使用Office 365管理API大约一年了,但是我从未遇到任何问题。最近,我在请求获取访问令牌时遇到以下错误。

HTTPSConnectionPool(host='manage.office.com', port=443): Max retries 
exceeded with url: /api/v1.0/2f6cb1a6-ecb8-4578-b680- 
bf84ded07ff4/activity/feed/subscriptions/list (Caused by 
SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate 
verify failed (_ssl.c:719)'),))

注意:请求并非总是失败。有时,他们经过,得到服务并获得访问令牌。有时他们无法给出上述错误。

以下是向office365发出请求的代码:

def get_headers(access_token=None, content_type=None):
    headers = {'User-Agent': 'Super Agent/0.0.1', 'Accept-Language': 'en-US,en;q=0.5'}
    if access_token:
        headers['Authorization'] = 'Bearer %s' % access_token
    if content_type:
        headers['Content-Type'] = content_type

    return headers


def get_access_token(resource_url):
    url = 'https://login.windows.net/' + APP_TENANT + '/oauth2/token'
    parameters = 'client_id=%s&client_secret=%s&resource=%s' % (APP_ID, APP_SECRET, resource_url)

    headers = get_headers()
    response = requests.post(url, headers=headers, data=parameters, timeout=REQUEST_TIMEOUT_VALUE)
    if response.status_code != 200:
        raise ValueError('response code not ok, %s' % response.text)

    access_token = response.json()['access_token']
    return access_token

access_token = get_access_token('https://manage.office.com&grant_type=client_credentials')

0 个答案:

没有答案