まず、私はあなたがおそらく言うことができるように、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(許可されていない)応答が返されます。
誰かが私が行方不明/間違っていることを知っていますか?
ありがとうございます!
私はasana oauthライブラリを使用しています。しかし、oauthlibライブラリのリクエストからのOAuth2Sessionはありません。私はあなたが代わりにここに与えられた例に従うべきだと思う - https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow – iamkhush