/Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/reddit-oauth/lib/index.js:278t-development-environment@1.0.0~preinstall: javascript-development-environment@1.0.0
if (query.state !== state || !query.code) {
^
TypeError: Cannot read property 'state' of undefined
at RedditApi__oAuthTokens [as oAuthTokens] (/Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/reddit-oauth/lib/index.js:278:18)
at Object.<anonymous> (/Users/sharanduggirala/Documents/UID/CS235ProjectJS/buildScripts/srcReddit.js:30:8)
at Module._compile (module.js:624:30)
at loader (/Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/babel-register/lib/node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (/Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/babel-register/lib/node.js:154:7)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at /Users/sharanduggirala/Documents/UID/CS235ProjectJS/node_modules/babel-cli/lib/_babel-node.js:159:24
我目前正在尝试使用Reddit API,并尝试让this页面上的示例正常运行。这是我在尝试编译以下代码时遇到的错误:
var RedditApi = require('reddit-oauth');
var request = require('request');
var reddit = new RedditApi({
app_id: **********,
app_secret: ********* ,
redirect_uri: 'http://localhost:8888'
});
// Authenticate with username/password
reddit.passAuth(
'sharan100',
'********',
function (success) {
if (success) {
// Print the access token we just retrieved
console.log(reddit.access_token);
}
}
);
// Get the OAuth URL to redirect users to
// Scopes are defined here: https://github.com/reddit/reddit/wiki/OAuth2
reddit.oAuthUrl('some_state', 'identity');
// After the user is redirected back to us, grab the query string
// object and exchange it for a set of access and refresh tokens.
// Scope has to be identical as the one provided to oAuthUrl. Can
// change for each authentication attempt.
reddit.oAuthTokens(
'some_state',
request.query,
function (success) {
// Print the access and refresh tokens we just retrieved
console.log(reddit.access_token);
console.log(reddit.refresh_token);
}
);
console.log('request')
的输出是:
{ [Function: request]
get: [Function],
head: [Function],
options: [Function],
post: [Function],
put: [Function],
patch: [Function],
del: [Function],
delete: [Function],
jar: [Function],
cookie: [Function],
defaults: [Function],
forever: [Function],
Request:
{ [Function: Request]
super_:
{ [Function: Stream]
super_: [Object],
Readable: [Object],
Writable: [Object],
Duplex: [Object],
Transform: [Object],
PassThrough: [Object],
Stream: [Circular],
_isUint8Array: [Function: isUint8Array],
_uint8ArrayToBuffer: [Function: _uint8ArrayToBuffer] },
debug: undefined,
defaultProxyHeaderWhiteList:
[ 'accept',
'accept-charset',
'accept-encoding',
'accept-language',
'accept-ranges',
'cache-control',
'content-encoding',
'content-language',
'content-location',
'content-md5',
'content-range',
'content-type',
'connection',
'date',
'expect',
'max-forwards',
'pragma',
'referer',
'te',
'user-agent',
'via' ],
defaultProxyHeaderExclusiveList: [ 'proxy-authorization' ] },
initParams: [Function: initParams],
debug: [Getter/Setter] }
注意:代码已经过编辑,以反映以下评论。
不确定为什么此代码似乎正在解决上述错误。
答案 0 :(得分:1)
我很确定这是无效的:
reddit.oAuthTokens(
'some_state',
// this
request.query,
function (success) {
// Print the access and refresh tokens we just retrieved
console.log(reddit.access_token);
console.log(reddit.refresh_token);
}
);
就像在做const request.query = 'asdf'
尝试改为(destructure query off request):
const { query } = request
// or: var query = request.query
reddit.oAuthTokens(
'some_state',
// this
query,
function (success) {
// Print the access and refresh tokens we just retrieved
console.log(reddit.access_token);
console.log(reddit.refresh_token);
}
);