3 _=\>@sddlmZddlZddlZddlmZddlmZddlZddl m Z m Z ddl m Z ddlmZdZGd d d eZGd d d eZGd dde ZGdddeZdS))print_functionN) TweepError)API) OAuth1SessionOAuth1)AuthBase)parse_qszWarning! Due to a Twitter API bug, signin_with_twitter and access_type don't always play nice together. Details https://dev.twitter.com/discussions/21281c@seZdZddZddZdS) AuthHandlercCstdS)z'Apply authentication headers to requestN)NotImplementedError)selfurlmethodheaders parametersrc/private/var/folders/47/550316q90kl84vt_jn5ctz8chwmm0c/T/pip-install-yoigbb1v/tweepy/tweepy/auth.py apply_authszAuthHandler.apply_authcCstdS)z-Return the username of the authenticated userN)r )r rrr get_usernameszAuthHandler.get_usernameN)__name__ __module__ __qualname__rrrrrrr sr c@sheZdZdZdZdZdddZddZd d Zdd d Z d dZ dddZ dddZ ddZ ddZdS) OAuthHandlerzOAuth authentication handlerzapi.twitter.comz/oauth/NcCsjt|tjkr|jd}t|tjkr0|jd}||_||_d|_d|_||_d|_ t |||jd|_ dS)Nascii) client_secret callback_uri) typesix text_typeencode consumer_keyconsumer_secret access_tokenaccess_token_secretcallbackusernameroauth)r rr r#rrr__init__"s  zOAuthHandler.__init__cCsd|j|j|S)Nzhttps://) OAUTH_HOST OAUTH_ROOT)r endpointrrr_get_oauth_url3szOAuthHandler._get_oauth_urlcCst|j|j|j|jddS)N)rresource_owner_keyresource_owner_secretZdecoding)rrr r!r")r rrrr6s zOAuthHandler.apply_authcCsVy&|jd}|r|d|7}|jj|Stk rP}zt|WYdd}~XnXdS)N request_tokenz?x_auth_access_type=%s)r*r%Zfetch_request_token Exceptionr)r access_typer errr_get_request_token=s   zOAuthHandler._get_request_tokencCs||_||_dS)N)r!r")r keyZsecretrrrset_access_tokenFszOAuthHandler.set_access_tokenFcCsryB|r |jd}|r*tjtn |jd}|j|d|_|jj|Stk rl}zt |WYdd}~XnXdS)z.Get the authorization URL to redirect the userZ authenticateZ authorize)r/N) r*loggingwarningWARNING_MESSAGEr1r-r%Zauthorization_urlr.r)r Zsignin_with_twitterr/r r0rrrget_authorization_urlJs    z"OAuthHandler.get_authorization_urlcCsy^|jd}t|j|j|jd|jd||jd|_|jj|}|d|_|d|_ |j|j fSt k r}zt |WYdd}~XnXdS)zt After user has authorized the request token, get access token with user supplied verifier. r! oauth_tokenoauth_token_secret)rr+r,verifierrN) r*rrr r-r#r%Zfetch_access_tokenr!r"r.r)r r:r respr0rrrget_access_tokenZs     zOAuthHandler.get_access_tokencCsyX|jd}t|j|jd}tj||d||dd}t|j}|jdd|jddfSt k r}zt |WYd d }~XnXd S) a  Get an access token from an username and password combination. In order to get this working you need to create an app at http://twitter.com/apps, after that send a mail to api@twitter.com and request activation of xAuth for it. r!)rZ client_auth)Z x_auth_modeZx_auth_usernameZx_auth_password)r authrr8rr9N) r*rrr requestspostrcontentgetr.r)r r$passwordr r%r credentialsr0rrrget_xauth_access_tokenms    z#OAuthHandler.get_xauth_access_tokencCs6|jdkr0t|}|j}|r(|j|_ntd|jS)Nz,Unable to get username, invalid oauth token!)r$rZverify_credentialsZ screen_namer)r apiuserrrrrs  zOAuthHandler.get_username)N)N)FN)N)rrr__doc__r'r(r&r*rr1r3r7r<rErrrrrrs    rc@seZdZddZddZdS) OAuth2BearercCs ||_dS)N) bearer_token)r rJrrrr&szOAuth2Bearer.__init__cCsd|j|jd<|S)NzBearer Authorization)rJr)r requestrrr__call__szOAuth2Bearer.__call__N)rrrr&rMrrrrrIsrIc@s0eZdZdZdZdZddZddZdd Zd S) AppAuthHandlerz'Application-only authentication handlerzapi.twitter.comz/oauth2/cCsj||_||_d|_tj|jd|j|jfddid}|j}|jddkr\td|jd|d |_dS) NtokenZ grant_typeZclient_credentials)r=data token_typeZbearerz9Expected token_type to equal "bearer", but got %s insteadr!) rr _bearer_tokenr>r?r*jsonrAr)r rr r;rQrrrr&s  zAppAuthHandler.__init__cCsd|j|j|S)Nzhttps://)r'r()r r)rrrr*szAppAuthHandler._get_oauth_urlcCs t|jS)N)rIrS)r rrrrszAppAuthHandler.apply_authN) rrrrHr'r(r&r*rrrrrrNs rN) __future__rrr4Z tweepy.errorrZ tweepy.apirr>Zrequests_oauthlibrrZ requests.authrZsix.moves.urllib.parserr6objectr rrIrNrrrrs      r