使用 python 个请求访问 ASANA 数据

Accesing ASANA data using python requests

首先,我不是一个 Python 大师,你可能会说...所以我们开始吧。

我正在尝试使用 Asana 的 API 通过 Python 请求(项目、任务等)提取数据并使用 Oauth 2.0 进行身份验证...我一直在尝试找到一个简单的 python 脚本作为开始,但我没有任何运气,我找不到一个像样的简单示例!

我已经创建了应用程序并获得了我的 client_secret 和 client_secret。但我真的不知道从哪里或如何开始......有人可以帮助我吗?

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 一起使用。他们的 python 文档没有帮助我。我正在尝试提取可用的项目及其代码颜色,以便稍后将它们绘制到 Web 浏览器中(对于不同项目及其各自颜色的高级视图 - 绿色、黄色或红色)

当我将 url (https://app.asana.com/api/1.0/users/me) 引入浏览器时,它返回 json 数据响应,但是当我尝试对脚本,它返回 401(未授权)响应。

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

谢谢!!!

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

您是否没有专门使用 Asana Python 客户端库来发出请求?您希望从 Asana 获取的所有数据(项目、任务等)都可以使用 Asana Python 库访问。您需要在库中查找所需的方法。例如,tasks 资源 can be found here. I think this approach will be easier (and less error-prone) than switching between the Asana lib and the Requests lib. The Asana lib is actually built on top of Requests (as seen here) 的方法。