尝试在Python 3.7中获取Onenote的访问令牌

时间:2019-01-04 14:33:37

标签: python access-token onenote onenote-api

您好,我想从代码流中获取访问令牌,如下所述:

OneNote authentication and permissions

我已经从第一步收到了代码

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript">

function reqListener () {
  console.log(this.responseText.substr(0,60)+"...");
  var myblob = new Blob([window.btoa(this.responseText)], {
    type: 'text/plain'
  });
  console.log(myblob);
}

var oReq = new XMLHttpRequest(); // "To read files that are siblings of the HTML document, use XMLHttpRequest"
oReq.overrideMimeType("text/plain");
oReq.addEventListener("load", reqListener);
oReq.open("GET", "big_buck_bunny_trailer_480p.srt");
oReq.send();

</script>
</head>
</html>

然后,我尝试使用该代码获取访问令牌:

https://your-redirect-uri?code=M57010781-9e8c-e31e-ca0d-46bc104236c4

“ new_url”中的值现在为

payload2 = {'grant_type' : 'authorization_code',
               'client_id': 'xxx-xxxx-xxxx-xxx-xxxx',
                'client_secret' : 'secret',
                'code' : 'M57010781-9e8c-e31e-ca0d-46bc104236c4',
               'redirect_uri' : 'https://login.live.com/oauth20_desktop.srf',}

        b_url = 'https://login.live.com/oauth20_token.srf?'
    new_tok_req = urlencode(payload2)
    new_url = b_url+new_tok_req

然后,我使用以下方法发送帖子:

https://login.live.com/oauth20_token.srf?grant_type=authorization_code&client_id=xxxxxxxxxx&client_secret=secret&code=M57010781-9e8c-e31e-ca0d-46bc104236c4&redirect_uri=https%253A%2F%2Flogin.live.com%2Foauth20_desktop.srf

这会触发服务器中的以下错误:

  

b'{“ error”:“ invalid_request”,“ error_description”:“提供的请求必须具有内容类型\'application / x-www-form-urlencoded \'。”}'

我找不到问题。 对我来说, r2 = requests.post(new_url) 中的URL格式正确。 请帮我。 预先谢谢你。

0 个答案:

没有答案