【问题标题】:Yahoo Oauth2 step 3雅虎 Oauth2 第 3 步
【发布时间】:2019-01-07 05:28:59
【问题描述】:

我正在尝试创建一个快速的网络应用程序来验证用户的 Yahoo 帐户,但我无法获得“用户批准”。

Yahoo Auth Page

就我个人而言,每次我去外部网站并且必须进行身份验证时,我通常都会登录我的帐户。这似乎将我重定向到一个页面并要求输入代码。我不知道我需要提供什么代码才能进行身份验证。如果我不知道,我的用户肯定不会!我正在构建一个烧瓶应用程序,并尝试围绕this repo 建模我的代码。

我已经为雅虎添加了一些代码,但似乎无法连接这些点。下面 oauth.py 文件中的新 YahooSignIn 子类:

class YahooSignIn(OAuthSignIn):
def __init__(self):
    super(YahooSignIn, self).__init__('yahoo')
    self.service = OAuth2Service(
        name='yahoo',
        consumer_id=self.consumer_id,
        consumer_secret=self.consume_secret,
        authorize_url='https://api.login.yahoo.com/oauth/v2/request_auth',
        access_token_url='https://api.login.yahoo.com/oauth/v2/get_token',
        base_url='http://fantasysports.yahooapis.com/'
    )

def authorize(self):
    return redirect(self.service.get_authorize_url(
        scope='email',
        response_type='code',
        redirect_uri=self.get_callback_url())
    )

def callback(self):
    def decode_json(payload):
        return json.loads(payload.decode('utf-8'))

    if 'code' not in request.args:
        return None, None, None
    oauth_session = self.service.get_auth_session(
        data={'code': request.args['code'],
              'grant_type': 'authorization_code',
              'redirect_uri': self.get_callback_url()},
        decoder=decode_json
    )
    me = oauth_session.get('me?fields=id,email').json()
    return (
        'yahoo$' + me['id'],
        me.get('email').split('@')[0],
        me.get('email')
    )

唯一的其他更改是对 index.html 页面添加了一个带有“yahoo”参数的附加链接

<p><a href="{{ url_for('oauth_authorize', provider='yahoo') }}">Login with Yahoo</a></p>

任何帮助都将不胜感激,因为在过去的两个晚上,这个问题一直困扰着我,我很想摆脱这个问题!

【问题讨论】:

    标签: python flask oauth-2.0 yahoo


    【解决方案1】:

    在今年(2018/19)之前,我一直在使用 Yahoo 的 Oauth 1.0 API。今年我在使用它时遇到了问题,所以我通过下面链接的 yahoo-oauth 库切换到使用 Oauth 2.0。他们有一个很好的页面,描述了如何使用他们的库。这是我使用的代码。

    from yahoo_oauth import OAuth2
    
    class YahooFantasyAPI:
    
      def fetchGameID(self):
        session = self.getSession()
        r = session.get(
        'https://fantasysports.yahooapis.com/fantasy/v2/game/nfl'
        )
        print(r.text)
    
      def getSession(self):
        oauth = OAuth2(None, None, from_file='oauth2.json')
    
        if not oauth.token_is_valid():
            oauth.refresh_access_token()
    
        return oauth.session
    
    api = YahooFantasyAPI()
    fetchGameID()
    

    https://yahoo-oauth.readthedocs.io/en/latest/

    【讨论】:

      猜你喜欢
      • 2015-03-03
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      相关资源
      最近更新 更多