搜尋套件、儲存庫與 APP 分類

套件詳情v1.1.0

draw

一個功能完整的 Reddit API 包裝器,適用於 Dart,靈感來自 PRAW。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
113
喜歡數
21
Pub 分數
135

安裝套件

flutter pub add draw

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Draw 是一個功能完整的 Dart Reddit API 包裝程式,專為簡化與 Reddit API 的互動而設計。受到 PRAW(Python Reddit API Wrapper)的啟發,它提供了直覺且強大的介面,用於存取 Reddit 資料,包括文章、評論、使用者資訊以及子版塊細節。此套件支援所有主要平台——Android、iOS、Web、macOS、Windows 和 Linux,非常適合跨平台應用程式。透過強大的驗證處理與非同步作業,Draw 讓開發者能有效率地建構整合 Reddit 的應用程式。

適用情境

  • 建構 Reddit 客戶端
  • 獲取熱門貼文
  • 使用者和子版塊互動
  • 評論審核工具
  • 即時訊息更新

主要特色

  • 完整的 Reddit API 覆蓋
  • 跨平台支援
  • 非同步/等待整合
  • OAuth2 認證
  • 易於使用的物件模型

適合對象

  • Dart 開發人員
  • Reddit 應用創建者
  • 社群媒體整合工具
  • API 入門
  • 跨平台專案

使用建議

  • 需要 Reddit 應用憑證
  • 速率限制意識
  • 需要處理 OAuth 流程
  • 僅限公開資料,無需權限
  • 依賴Reddit的API穩定性

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

DRAW: The Dart Reddit API Wrapper

Build Status Pub Version Coverage Status

DRAW, also known as the Dart Reddit API Wrapper, is a Dart package that provides simple access to the Reddit API. DRAW is inspired by PRAW, the Python Reddit API Wrapper, and aims to also maintain a similar interface.

Want to get involved? Check out how to contribute to get started!

Disclaimer: This is not an official Google product.

Installation

Installing DRAW is simple using Dart's package management system, pub. Instructions on how to import DRAW into your project can be found here. If you would prefer to live on the hemorrhaging-edge, methods to depend on a local copy of DRAW or on the Github repository can be found here.

Getting Started

Assuming you already have your Reddit OAuth credentials, getting started with DRAW is simple:

import 'dart:async';
import 'package:draw/draw.dart';

Future<void> main() async {
  // Create the `Reddit` instance and authenticated
  Reddit reddit = await Reddit.createScriptInstance(
    clientId: CLIENT_ID,
    clientSecret: SECRET,
    userAgent: AGENT_NAME,
    username: "DRAWApiOfficial",
    password: "hunter12", // Fake
  );

  // Retrieve information for the currently authenticated user
  Redditor currentUser = await reddit.user.me();
  // Outputs: My name is DRAWApiOfficial
  print("My name is ${currentUser.displayName}");
}

This simple example is a great way to confirm that DRAW is working and that your credentials have been configured correctly.

Web Authentication

To authenticate via the Reddit authentication page, the web authentication flow needs to be used. This requires that a web application is registered with a valid Reddit account, which provides a client-id and a client-secret. As part of this process, a redirect URL is associated with the registered web application. These three values are all that is needed to complete the web authentication flow.

Here is a simple example of how to use web authentication with DRAW:

import 'package:draw/draw.dart';

main() async {
  final userAgent = 'foobar';
  final configUri = Uri.parse('draw.ini');

  // Create a `Reddit` instance using a configuration file in the
  // current directory.
  final reddit = Reddit.createWebFlowInstance(userAgent: userAgent,
                                              configUri: configUri);

  // Build the URL used for authentication. See `WebAuthenticator`
  // documentation for parameters.
  final auth_url = reddit.auth.url(['*'], 'foobar');

  // ...
  // Complete authentication at `auth_url` in the browser and retrieve
  // the `code` query parameter from the redirect URL.
  // ...

  // Assuming the `code` query parameter is stored in a variable
  // `auth_code`, we pass it to the `authorize` method in the
  // `WebAuthenticator`.
  await reddit.auth.authorize(auth_code);

  // If everything worked correctly, we should be able to retrieve
  // information about the authenticated account.
  print(await reddit.user.me());
}

It is also possible to restore cached credentials in order to avoid the need to complete the web authentication flow on each run:

import 'package:draw/draw.dart';

// Provides methods to load and save credentials.
import 'credential_loader.dart';

main() async {
  final userAgent = 'foobar';
  final configUri = Uri.parse('draw.ini');

  // Load cached credentials from disk, if available.
  final credentialsJson = await loadCredentials();

  var reddit;

  if (credentialsJson == null) {
    reddit =
        await Reddit.createWebFlowInstance(userAgent: userAgent,
                                           configUri: configUri);

    // Build the URL used for authentication. See `WebAuthenticator`
    // documentation for parameters.
    final auth_url = reddit.auth.url(['*'], 'foobar');

    // ...
    // Complete authentication at `auth_url` in the browser and retrieve
    // the `code` query parameter from the redirect URL.
    // ...

    // Assuming the `code` query parameter is stored in a variable
    // `auth_code`, we pass it to the `authorize` method in the
    // `WebAuthenticator`.
    await reddit.auth.authorize(auth_code);

    // Write credentials to disk.
    await writeCredentials(reddit.auth.credentials.toJson());
  } else {
    // Create a new Reddit instance using previously cached credentials.
    reddit = Reddit.restoreAuthenticatedInstance(
        userAgent: userAgent,
        configUri: configUri,
        credentialsJson: credentialsJson);
  }

  // If everything worked correctly, we should be able to retrieve
  // information about the authenticated account.
  print(await reddit.user.me());
}

Installed Application Authentication

For usage in environments where it is impossible to keep a client secret secure, the installed application flow should be used. This requires that an installed application is registered with a valid Reddit account, which provides a client-id. As part of this process, a redirect URL is associated with the registered installed application. These two values are all that is needed to complete the installed application authentication flow.

The installed application authentication flow is almost identical to the web authentication flow described above, and it is also possible to save and restore credentials for installed applications in a similar fashion.

Read up more about how to use this authentication flow on here.

Here is a simple example of how to use the installed application authentication flow with DRAW:

import 'package:draw/draw.dart';

main() async {
  final userAgent = 'foobar';
  final configUri = Uri.parse('draw.ini');

  // Create a `Reddit` instance using a configuration file in the current
  // directory. Unlike the web authentication example, a client secret does
  // not need to be provided in the configuration file.
  final reddit = Reddit.createInstalledFlowInstance(userAgent: userAgent,
                                                    configUri: configUri);

  // Build the URL used for authentication. See `WebAuthenticator`
  // documentation for parameters.
  final auth_url = reddit.auth.url(['*'], 'foobar');

  // ...
  // Complete authentication at `auth_url` in the browser and retrieve
  // the `code` query parameter from the redirect URL.
  // ...

  // Assuming the `code` query parameter is stored in a variable
  // `auth_code`, we pass it to the `authorize` method in the
  // `WebAuthenticator`.
  await reddit.auth.authorize(auth_code);

  // If everything worked correctly, we should be able to retrieve
  // information about the authenticated account.
  print(await reddit.user.me());
}

DRAW Configuration Files (draw.ini)

Here's an example draw.ini suitable for web based authentication:

default=default
reddit_url='https://www.reddit.com'
oauth_url=https://oauth.reddit.com
redirect_uri=https://www.google.com
client_id=YOUR_CLIENT_ID_HERE
client_secret=YOUR_SECRET_HERE
userAgent=draw_testing_agent

Here the redirect URI is set to https://www.google.com, but you'll need to replace that with whatever redirect you have registered.

The format of draw.ini configuration files is very similar to that of praw.ini files used by PRAW, although there may be some minor differences due to the .ini parser used by DRAW.

Frequently Asked Questions (FAQ)

Q: "I'm having trouble authenticating. What's wrong?"

Assuming the build status of DRAW is passing, there's likely something wrong with your credentials or user-agent. Here's some things to check:

  • Ensure your client ID and client secret match those provided by Reddit, applicable for your use case.
  • Try a new value for userAgent. Reddit rejects requests with commonly used user-agents like "foobar", "testing", or "reddit", so try using a randomly generated user-agent to make sure this isn't the issue you're seeing.

License

DRAW is provided under a BSD 3-clause license. Copyright (c), 2017, the DRAW Project Authors and Google LLC.

所屬分類

依賴

依賴清單 9 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。