コード例 #1
0
async def main():
    """Provide the program's entry point when directly executed."""
    requestor = asyncprawcore.Requestor("asyncprawcore_script_auth_example")

    try:
        authenticator = asyncprawcore.TrustedAuthenticator(
            requestor,
            os.environ["asyncprawcore_CLIENT_ID"],
            os.environ["asyncprawcore_CLIENT_SECRET"],
        )
        authorizer = asyncprawcore.ScriptAuthorizer(
            authenticator,
            os.environ["asyncprawcore_USERNAME"],
            os.environ["asyncprawcore_PASSWORD"],
        )
        await authorizer.refresh()

        async with asyncprawcore.session(authorizer) as session:
            data = await session.request("GET", "/api/v1/me/friends")

        for friend in data["data"]["children"]:
            print(friend["name"])

        return 0
    finally:
        await requestor.close()
コード例 #2
0
 async def test_refresh__with_invalid_username_or_password(self):
     authorizer = asyncprawcore.ScriptAuthorizer(self.authentication,
                                                 USERNAME,
                                                 "invalidpassword")
     with VCR.use_cassette(
             "ScriptAuthorizer_refresh__with_invalid_username_or_password"):
         with self.assertRaises(asyncprawcore.OAuthException):
             await authorizer.refresh()
         self.assertFalse(authorizer.is_valid())
コード例 #3
0
async def script_authorizer():
    requestor = asyncprawcore.requestor.Requestor(
        "asyncprawcore:test (by /u/Lil_SpazJoekp)")
    authenticator = asyncprawcore.TrustedAuthenticator(requestor, CLIENT_ID,
                                                       CLIENT_SECRET)
    authorizer = asyncprawcore.ScriptAuthorizer(authenticator, USERNAME,
                                                PASSWORD, two_factor_callback)
    await authorizer.refresh()
    return authorizer
コード例 #4
0
    async def test_refresh_with__invalid_otp(self):
        authorizer = asyncprawcore.ScriptAuthorizer(
            self.authentication,
            USERNAME,
            PASSWORD,
            lambda: "fake",
        )

        with VCR.use_cassette("ScriptAuthorizer_refresh_with__invalid_otp"):
            with self.assertRaises(asyncprawcore.OAuthException):
                await authorizer.refresh()
            self.assertFalse(authorizer.is_valid())
コード例 #5
0
    async def test_refresh(self):
        authorizer = asyncprawcore.ScriptAuthorizer(self.authentication,
                                                    USERNAME, PASSWORD)
        self.assertIsNone(authorizer.access_token)
        self.assertIsNone(authorizer.scopes)
        self.assertFalse(authorizer.is_valid())

        with VCR.use_cassette("ScriptAuthorizer_refresh"):
            await authorizer.refresh()

        self.assertIsNotNone(authorizer.access_token)
        self.assertEqual({"*"}, authorizer.scopes)
        self.assertTrue(authorizer.is_valid())
コード例 #6
0
    async def test_refresh_with__valid_otp(self):
        authorizer = asyncprawcore.ScriptAuthorizer(self.authentication,
                                                    USERNAME, PASSWORD,
                                                    lambda: "000000")
        self.assertIsNone(authorizer.access_token)
        self.assertIsNone(authorizer.scopes)
        self.assertFalse(authorizer.is_valid())

        with VCR.use_cassette("ScriptAuthorizer_refresh_with__valid_otp"):
            await authorizer.refresh()

        self.assertIsNotNone(authorizer.access_token)
        self.assertEqual(set(["*"]), authorizer.scopes)
        self.assertTrue(authorizer.is_valid())