2017-05-09 41 views
1

まず、私はあなたがおそらく言うことができるように、Pythonの達人ではありません...だからここで行きます。Pythonリクエストを使ってASANAデータにアクセスする

私はPythonリクエスト(プロジェクト、タスクなど)でデータを取得し、Oauth 2.0を使用してデータを取得するためにAsanaのAPIを使用しようとしています...私は単純な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でリクエストlibをどのように使用するかわかりません。彼らのpythonのドキュメントは私を助けなかった。私は利用可能なプロジェクトとそのコードの色を引き出し、後でそれらをWebブラウザにプロットすることができます(さまざまなプロジェクトとそれぞれの色(緑色、黄色、または赤色)の高レベルのビューについては

)私はブラウザにURL(https://app.asana.com/api/1.0/users/me)を導入すると、データでjson応答を返しますが、スクリプトで同じことをしようとすると、401(許可されていない)応答が返されます。

誰かが私が行方不明/間違っていることを知っていますか?

ありがとうございます!

+1

私はasana oauthライブラリを使用しています。しかし、oauthlibライブラリのリクエストからのOAuth2Sessionはありません。私はあなたが代わりにここに与えられた例に従うべきだと思う - https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow – iamkhush

答えて

0

私は、Requestsライブラリが低レベルのライブラリであると考えています。すべてのパラメータをリクエストに渡す必要があります。

Asana Pythonクライアントライブラリを使用してリクエストを行うだけではない理由はありますか? Asana(プロジェクト、タスクなど)からフェッチするすべてのデータは、Asana Pythonライブラリを使用してアクセスできます。あなたは必要な方法を見つけるために図書館を見たいと思うでしょう。例えば、tasksリソースcan be found hereのメソッド。私はこのアプローチがAsanaのlibとRequestsのlibを切り替えるよりも簡単で(エラーが起こりにくい)と考えています。 Asanaライブラリは実際にリクエストの上に構築されています(as seen here)。

+0

提供されたリンクありがとう。彼らは多くの助けになった!乾杯。 – Juancho

関連する問題