@@ -7,6 +7,12 @@ export const getPostsTool: ToolConfig<RedditPostsParams, RedditPostsResponse> =
77 description : 'Fetch posts from a subreddit with different sorting options' ,
88 version : '1.0.0' ,
99
10+ oauth : {
11+ required : true ,
12+ provider : 'reddit' ,
13+ additionalScopes : [ 'read' ] ,
14+ } ,
15+
1016 params : {
1117 subreddit : {
1218 type : 'string' ,
@@ -38,8 +44,8 @@ export const getPostsTool: ToolConfig<RedditPostsParams, RedditPostsResponse> =
3844 const sort = params . sort || 'hot'
3945 const limit = Math . min ( Math . max ( 1 , params . limit || 10 ) , 100 )
4046
41- // Build URL with appropriate parameters
42- let url = `https://www .reddit.com/r/${ subreddit } /${ sort } .json ?limit=${ limit } &raw_json=1`
47+ // Build URL with appropriate parameters using OAuth endpoint
48+ let url = `https://oauth .reddit.com/r/${ subreddit } /${ sort } ?limit=${ limit } &raw_json=1`
4349
4450 // Add time parameter only for 'top' sorting
4551 if ( sort === 'top' && params . time ) {
@@ -49,29 +55,54 @@ export const getPostsTool: ToolConfig<RedditPostsParams, RedditPostsResponse> =
4955 return url
5056 } ,
5157 method : 'GET' ,
52- headers : ( ) => ( {
53- 'User-Agent' :
54- 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36' ,
55- Accept : 'application/json' ,
56- } ) ,
58+ headers : ( params : RedditPostsParams ) => {
59+ if ( ! params . accessToken ) {
60+ throw new Error ( 'Access token is required for Reddit API' )
61+ }
62+
63+ return {
64+ Authorization : `Bearer ${ params . accessToken } ` ,
65+ 'User-Agent' : 'sim-studio/1.0 (https://github.com/simstudioai/sim)' ,
66+ Accept : 'application/json' ,
67+ }
68+ } ,
5769 } ,
5870
5971 transformResponse : async ( response : Response , requestParams ?: RedditPostsParams ) => {
6072 try {
6173 // Check if response is OK
6274 if ( ! response . ok ) {
75+ // Get response text for better error details
76+ const errorText = await response . text ( )
77+ console . error ( 'Reddit API Error:' , {
78+ status : response . status ,
79+ statusText : response . statusText ,
80+ body : errorText ,
81+ url : response . url ,
82+ } )
83+
6384 if ( response . status === 403 || response . status === 429 ) {
6485 throw new Error ( 'Reddit API access blocked or rate limited. Please try again later.' )
6586 }
66- throw new Error ( `Reddit API returned ${ response . status } : ${ response . statusText } ` )
87+ throw new Error (
88+ `Reddit API returned ${ response . status } : ${ response . statusText } . Body: ${ errorText } `
89+ )
6790 }
6891
6992 // Attempt to parse JSON
7093 let data
7194 try {
7295 data = await response . json ( )
73- } catch ( _error ) {
74- throw new Error ( 'Failed to parse Reddit API response: Response was not valid JSON' )
96+ } catch ( error ) {
97+ const responseText = await response . text ( )
98+ console . error ( 'Failed to parse Reddit API response as JSON:' , {
99+ error : error instanceof Error ? error . message : String ( error ) ,
100+ responseText,
101+ contentType : response . headers . get ( 'content-type' ) ,
102+ } )
103+ throw new Error (
104+ `Failed to parse Reddit API response: Response was not valid JSON. Content: ${ responseText } `
105+ )
75106 }
76107
77108 // Check if response contains error
0 commit comments