Draw는 Dart용 포괄적인 Reddit API 래퍼로, Reddit API와의 상호작용을 간편하게 만듭니다. PRAW(Python Reddit API Wrapper)를 영감으로 받아 게시물, 댓글, 사용자 정보, 서브레딧 세부 정보 등 Reddit 데이터에 접근하기 위한 직관적이고 강력한 인터페이스를 제공합니다. 이 패키지는 Android, iOS, Web, macOS, Windows, Linux 등 모든 주요 플랫폼을 지원하여 크로스플랫폼 애플리케이션 개발에 이상적입니다. 강력한 인증 처리 및 비동기 작업을 통해 개발자는 효율적으로 Reddit 통합 앱을 구축할 수 있습니다.
사용 사례
Reddit 클라이언트 만들기
트렌드 게시물 가져오는 중
사용자 및 서브레딧 상호작용
댓글 관리 도구
실시간 피드 업데이트
주요 기능
완전한 Reddit API 커버리지
크로스 플랫폼 지원
비동기/대기 통합
OAuth2 인증
사용하기 쉬운 객체 모델
적합한 대상
Dart 개발자
Reddit 앱 제작자
소셜 미디어 통합기
API 초보자
크로스 플랫폼 프로젝트
고려 사항
Reddit 앱 자격 증명이 필요합니다
레이트 제한 인식
OAuth 흐름을 처리해야 합니다
권한 없이 공개 데이터에 제한됨
Reddit의 API 안정성에 따라 다름
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
DRAW: The Dart Reddit API Wrapper
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.
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.
import'dart:async';
import'package:draw/draw.dart';
Future<void> main() async {
// Create the `Reddit` instance and authenticatedReddit reddit = awaitReddit.createScriptInstance(
clientId: CLIENT_ID,
clientSecret: SECRET,
userAgent: AGENT_NAME,
username: "DRAWApiOfficial",
password: "hunter12", // Fake
);
// Retrieve information for the currently authenticated userRedditor currentUser = await reddit.user.me();
// Outputs: My name is DRAWApiOfficialprint("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 = awaitloadCredentials();
var reddit;
if (credentialsJson == null) {
reddit =
awaitReddit.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.awaitwriteCredentials(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:
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.