第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何以編程方式將數(shù)據(jù)輸入到 python 'input()' 提示中?

如何以編程方式將數(shù)據(jù)輸入到 python 'input()' 提示中?

忽然笑 2021-11-23 19:34:29
我正在為 Spotify API 使用 python 包裝器。https://spotipy.readthedocs.io/en/latest/#installation作為授權(quán)過程的一部分,Spotify API 讓用戶在其默認 Web 瀏覽器中登錄 Spotify,然后將它們發(fā)送到預(yù)定義的(當(dāng)您向 Spotify 注冊應(yīng)用程序時)REDIRECT_URI。此 REDIRECT_URI 當(dāng)前設(shè)置為 localhost:6969。登錄后,它會生成一個 input()(死亡輸入),供您將重定向到的 URI 復(fù)制粘貼到命令提示符中。我真的不想在使用命令提示符時向隨機的人出售。目標(biāo)是在 localhost:6969 (起訴我)上打開一個燒瓶服務(wù)器,然后雙擊它的 /authorize 頁面(發(fā)送到 oauth2 登錄然后捕獲代碼)http://flask.pocoo.org/docs/1.0/所以 -我在 jflask.py 中保留我的 Flask 裝備和 Spotify 捕手:from flask import Flask, request, redirect, sessionfrom requests_oauth2 import OAuth2from spotipy import Spotifyimport spotipy.util as utilimport requestsimport datetimeapp = Flask(__name__)spotify_auth = OAuth2(  client_id='e33c6fa0d6a249ccafa232a9cf62a616',  client_secret='a43b0b6a4de14b97b4e468459e0f7824',  redirect_uri="http://localhost:6969/authorize",  site="https://accounts.spotify.com",  authorization_url="/authorize",  token_url="/api/token",  scope_sep=" ")# and then use this url as a link within a public login viewprint(spotify_auth.authorize_url(  scope=["user-read-recently-played", "user-top-read"],  response_type="code"  ))# and have an authorize route that can direct you to Spotify or handle you #being redirected back here from Spotify@app .route('/authorize')def authorize():  print('triggered')  error = request.args.get("error")  if error:    abort(404)  code = request.args.get("code")  if code:    data = spotify_auth.get_token(      code=code,      grant_type="authorization_code",    )    access_token = data["access_token"]    refresh_token = data["refresh_token"]    scopes = data["scope"].split()    self.sp = spotipy.Spotify(auth=access_token)    print(access_token)    return self.sp我已經(jīng)嘗試將 Popen 與 stdout=PIPE 一起使用。經(jīng)過進一步的研究,我很確定這也阻礙了我的成功。該規(guī)范旨在與 subprocess.communicate() 一起使用https://docs.python.org/3/library/subprocess.html#subprocess.PIP感謝大家的幫助。
查看完整描述

2 回答

?
慕田峪9158850

TA貢獻1794條經(jīng)驗 獲得超8個贊

Spotipy 的prompt_for_user_token方法是作為一種快速而骯臟的方法提供的,以允許在本地使用該模塊的人啟動和運行,并且如果您希望任意用戶能夠通過網(wǎng)站對自己進行身份驗證,則它不打算成為應(yīng)該直接構(gòu)建的代碼. 由于您有一個 Flask 應(yīng)用程序,您可能應(yīng)該擁有它和您的 Spotipy 代碼直接通過導(dǎo)入而不是使用管道和標(biāo)準(zhǔn) i/o 進行交互。

需要研究的一些事情是:

  • Flask-Login如果您想管理登錄網(wǎng)站的用戶

  • 將身份驗證傳遞給spotipy.Spotify訪問令牌(客戶端憑據(jù)管理器或requests會話)以外的替代方法。這些其他方法允許 Spotipy 客戶端在訪問令牌在使用中過期時刷新訪問令牌,處理用戶撤銷等。

  • requests-oauth2(請注意,這只是眾多 OAuth2 Python 庫中的一個,可能不是最好的),它使所有授權(quán)內(nèi)容更加方便,并允許您執(zhí)行以下操作:

# import Flask things, requests-oauth2, 

from requests_oauth2 import OAuth2

from spotipy import Spotify

from flask import redirect


spotify_auth = OAuth2(

    client_id=os.environ["SPOTIFY_CLIENT_ID"],

    client_secret=os.environ["SPOTIFY_CLIENT_SECRET"],

    redirect_uri=f"http://{os.environ['HOST_NAME']}/authorize",

    site="https://accounts.spotify.com",

    authorization_url="/authorize",

    token_url="/api/token",

    scope_sep=" "

)


# and then use this url as a link within a public login view

spotify_auth.authorize_url(

    scope=["user-read-recently-played", "user-top-read"],

    response_type="code"

)


# and have an authorize route that can direct you to Spotify or handle you being redirected back here from Spotify

@app.route('/authorize')

def authorize():


    error = request.args.get("error")

    if error:

        abort(404)


    code = request.args.get("code")

    if code:

        data = spotify_auth.get_token(

            code=code,

            grant_type="authorization_code",

        )

        access_token = data["access_token"]

        refresh_token = data["refresh_token"]

        scopes = data["scope"].split()

        spotify_client = spotipy.Spotify(auth=access_token)

    else:

        return redirect(spotify_auth.authorize_url(

            scope=["user-read-recently-played", "user-top-read"],

            response_type="code",

            state=session['state']

        ))

在第一次訪問 時/authorize,它會將用戶重定向到 Spotify 的登錄頁面。但是當(dāng)用戶在那里成功登錄時,它會將用戶重定向回 Flask 站點(返回/authorize),而不是執(zhí)行prompt_for_user_token().

一旦它們重新打開/authorize,這次有一個code請求參數(shù) - 因此它會發(fā)出 OAuth2 客戶端到提供者請求來將此代碼轉(zhuǎn)換為您的訪問令牌。

這兩者都prompt_for_user_token遵循相同的方法:

  1. 獲取 Spotify 授權(quán) URL 以引導(dǎo)用戶登錄

  2. 處理來自 Spotify 登錄的重定向 - Flask 通過接收來自 Spotify 重定向到的 URL 的輸入來prompt_for_user_token執(zhí)行此操作,并通過讓您從不存在的網(wǎng)頁復(fù)制/粘貼 URL 來執(zhí)行此操作。

  3. 調(diào)用以將代碼轉(zhuǎn)換為用戶的訪問和刷新令牌。

我可能對這段代碼做了一些修改,因為它是從我的 Spotify 集成的舊 Flask 實現(xiàn)中精心挑選出來的。這是一個更完整的要點,但我很確定很多 SQLAlchemy 的東西,授權(quán)視圖,非常糟糕,所以請加一點鹽。


查看完整回答
反對 回復(fù) 2021-11-23
?
侃侃無極

TA貢獻2051條經(jīng)驗 獲得超10個贊

您可以嘗試修改 util 模塊。用 response = raw_input() 將 try-except 塊替換為:

response = sys.stdin.readline().strip()

如果尚未導(dǎo)入,請不要忘記導(dǎo)入 sys。

這應(yīng)該允許正常的 PIPE-ing。

或者您可以使用 pexpect 庫而不是 subprocess 模塊。它知道如何處理調(diào)整終端 fcntl 標(biāo)志或在 Windows 上使用 mscvrt 的輸入。

此外,當(dāng) PIPE-ing 數(shù)據(jù)時,不要忘記 raw_input()、input() 或 sys.stdin.readline() 在它們接收到適當(dāng)?shù)男形沧址安粫祷亍?quot;\r"、"\n" 或 "\r\n"。您之前是否使用授權(quán) URL 發(fā)送過?


查看完整回答
反對 回復(fù) 2021-11-23
  • 2 回答
  • 0 關(guān)注
  • 259 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號