首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python请求访问ASANA数据

使用python请求访问ASANA数据
EN

Stack Overflow用户
提问于 2017-05-09 11:58:25
回答 1查看 805关注 0票数 2

首先,我不是Python大师,你可能知道.所以我们开始吧。

我试图使用Asana的API使用Python请求(项目、任务等)来提取数据,并使用OAuth2.0进行身份验证。我一直试图找到一个简单的python脚本,但我没有任何运气,也找不到一个像样和简单的例子!

我已经创建了这个应用程序,并得到了我的client_secret和client_secret。但我不知道从哪里开始.有人能帮我吗?

代码语言:javascript
复制
import sys, os, requests

sys.path.append(os.path.dirname(os.path.dirname(__file__)))

import asana
import json
from six import print_
import requests_oauthlib
from requests_oauthlib import OAuth2Session

client_id=os.environ['ASANA_CLIENT_ID'],
client_secret=os.environ['ASANA_CLIENT_SECRET'],
        # this special redirect URI will prompt the user to copy/paste the code.
        # useful for command line scripts and other non-web apps
redirect_uri='urn:ietf:wg:oauth:2.0:oob'

if 'ASANA_CLIENT_ID' in os.environ:

    #Creates a client with previously obtained Oauth credentials#
    client = asana.Client.oauth(

        #Asana Client ID and Secret, set as a Windows environments to avoid hardcoding variables into the script#
        client_id=os.environ['ASANA_CLIENT_ID'],
        client_secret=os.environ['ASANA_CLIENT_SECRET'],
        # this special redirect URI will prompt the user to copy/paste the code.
        # useful for command line scripts and other non-web apps
        redirect_uri='urn:ietf:wg:oauth:2.0:oob'
        )
    print ("authorized=", client.session.authorized)

    # get an authorization URL:

    (url, state) = client.session.authorization_url()


    try:
        # in a web app you'd redirect the user to this URL when they take action to
        # login with Asana or connect their account to Asana
        import webbrowser
        webbrowser.open(url)
    except Exception as e:
        print_("Open the following URL in a browser to authorize:")
        print_(url)

    print_("Copy and paste the returned code from the browser and press enter:")

    code = sys.stdin.readline().strip()
    # exchange the code for a bearer token
    token = client.session.fetch_token(code=code)

    #print_("token=", json.dumps(token))
    print_("authorized=", client.session.authorized)

me = client.users.me()


print "Hello " + me['name'] + "\n"

params = {'client_id' : client_id, 'redirect_uri' : redirect_uri, 'response_type' : token,}

print_("*************** Request begings *******************"+"\n")
print_("r = requests.get('https://app.asana.com/api/1.0/users/me)" + "\n")
r = requests.get('https://app.asana.com/api/1.0/users/me', params)

print_(r)
print_(r.json)
print_(r.encoding)

workspace_id = me['workspaces'][0]['id']
print_("My workspace ID is" + "\n")
print_(workspace_id)

print_(client.options)

我不知道如何在Asana中使用请求库。他们的蟒蛇医生帮不了我。我正在尝试拉出可用的项目和它们的代码颜色,以便以后可以将它们绘制到web浏览器中(用于查看不同的项目及其各自的颜色-绿色、黄色或红色)。

当我将url (https://app.asana.com/api/1.0/users/me)引入浏览器时,它会用数据返回json响应,但当我试图对脚本执行同样的操作时,它会给我回一个401 (未授权)响应。

有人知道我错过了什么/做错了什么吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-09 18:26:25

我认为问题在于请求库是一个较低级别的库。您需要将所有参数传递给请求。

您不完全使用Asana Python客户端库进行请求有原因吗?您希望从Asana获取的所有数据(项目、任务等)可以使用Asana Python库进行访问。您将希望在库中查找所需的方法。例如,tasks资源可以在这里找到的方法。我认为这种方法比在Asana库和请求库之间切换更容易(而且更容易出错)。Asana库实际上构建在请求(如图所示)之上。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43869206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档