使用python请求访问ASANA数据

问题描述:

首先,我不是一个Python大师,因为你可能会告诉...所以我们走吧。使用python请求访问ASANA数据

我想使用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(未授权)响应。

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

谢谢!!!

+1

我看到你使用asana oauth库。但不是来自requests-oauthlib库的OAuth2Session。我想你应该按照这里给出的例子 - https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow – iamkhush

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

是否有一个原因,你不是专门使用Asana Python客户端库来提出请求?您希望从Asana(项目,任务等)获取的所有数据都可以使用Asana Python库访问。您将希望在图书馆中查找您需要的方法。例如,tasks资源can be found here的方法。我认为这种方法比在Asana库和请求库之间切换更容易(并且不易出错)。 Asana lib实际上建立在请求(as seen here)之上。

+0

感谢您提供的链接。他们确实帮了大忙!干杯。 – Juancho