Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Still Getting Bad Request (HTTP 400). Failed to get an access token Despite 2.6 Update #28

Closed
dkomd opened this issue Aug 3, 2016 · 12 comments

Comments

@dkomd
Copy link

dkomd commented Aug 3, 2016

Hi,
I am using the following code which is the updated code for 2.6V..

app <-  oauth_app('facebook', 'XXXX', 'XXXXX')

Sys.setenv('HTTR_SERVER_PORT' = '1410/')
tkn <- oauth2.0_token(oauth_endpoints('facebook'), app, scope = 'ads_management',
    type  = 'application/x-www-form-urlencoded', cache = FALSE)

tkn <- tkn$credentials$access_token 

fbad_init(accountid = accountid, token = tkn, version = '2.6')

fbad_list_ad(fields = c('name', 'effective_status'))
fbad_list_ad(statuses = 'ACTIVE', fields = 'name')

However, I still get the same error:

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in init_oauth2.0(self$endpoint, self$app, scope = self$params$scope, :
Bad Request (HTTP 400). Failed to get an access token.

Error: object 'tkn' not found

Any thoughts/ideas on why is this happening?

Thanks!

@daroczig
Copy link
Owner

daroczig commented Aug 9, 2016

Did you set the "valid oauth redirect URL" as described in point (4)?

Pls also try what other's have suggested at #20 (comment) -- although I've tested it with the port setting and worked fine, but I can imagine that in some environments it works the other way.

@tokumotion
Copy link

Yes, the valid oauth redirect URL defaults to the address given before the UI changes. Also did run the port setting.

Gonna try creating a new app.

@daroczig
Copy link
Owner

daroczig commented Aug 9, 2016

Thanks, let me know how it works. If still having issues, pls share your sessionInfo and I will try to replicate the issue.

@tokumotion
Copy link

tokumotion commented Aug 9, 2016

Same problem with new app, attaching session info. It is weird, because in the browser I give the permissions but I don't get the token.

> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] httr_1.2.1

loaded via a namespace (and not attached):
[1] R6_2.1.2        rsconnect_0.4.3 tools_3.3.1     curl_1.1        Rcpp_0.12.6    
[6] httpuv_1.3.3

@dkomd
Copy link
Author

dkomd commented Aug 9, 2016

I tried previously what you said and it didn't work. However, today when I tried I got a different error completely.

> library(httr)
> 
> app <-  oauth_app('facebook', 'xxxx', 'xxxx')
> 
> 
> tkn <- oauth2.0_token(oauth_endpoints('facebook'), app, scope = 'ads_management',
+     type  = "application/x-www-form-urlencoded", cache = FALSE)
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
> 
> tkn <- tkn$credentials$access_token 
> 
> 
> fbad_init(accountid = accountid, token = tkn, version = '2.6')
Error in paste0("act_", accountid, "/") : object 'accountid' not found
> 
> 
> fbad_list_ad(fields = c('name', 'effective_status'))
Error in fbad_check_fbacc() : 
  Invalid R object passed as fbacc argument. See ?fbad_init for more details.
> fbad_list_ad(statuses = 'ACTIVE', fields = 'name')
Error in fbad_check_fbacc() : 
  Invalid R object passed as fbacc argument. See ?fbad_init for more details.
> 
> 

Here is the session info:

sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252

attached base packages:
[1] tools stats graphics grDevices utils datasets methods base

other attached packages:
[1] fbRads_0.2 httr_1.2.1 xlsx_0.5.7 xlsxjars_0.6.1 rJava_0.9-8

loaded via a namespace (and not attached):
[1] Rcpp_0.12.6 digest_0.6.9 bitops_1.0-6 plyr_1.8.4 chron_2.3-47
[6] R6_2.1.2 jsonlite_1.0 futile.options_1.0.0 curl_1.1 data.table_1.9.6
[11] futile.logger_1.4.3 lambda.r_1.1.9 bit64_0.9-5 RCurl_1.95-4.8 bit_1.1-12
[16] httpuv_1.3.3

@daroczig
Copy link
Owner

daroczig commented Aug 9, 2016

@dkomd you have to define accountid

@tokumotion thanks for the sessionInfo, the same version of httr works fine here on Linux, will try to get my hands on a Mac to see what the problem might be there

@tokumotion
Copy link

@daroczig Hi, it seems the problem comes from the update in the FB API.

This is the verbose error I get:

Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
-> POST /oauth/access_token HTTP/1.1
-> Host: graph.facebook.com
-> User-Agent: libcurl/7.43.0 r-curl/1.2 httr/1.2.1
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/x-www-form-urlencoded
-> Content-Length: 502
-> 
>> client_id=XXXX&redirect_uri=http%3A%2F%2Flocalhost%3A1410%2F%2F&grant_type=authorization_code&code=AQCY1HYgEkKoIf9iWadEx2jArH_b2hsfNGORROA7ciayMTlbTE0O69xe9k99zzyT9OTQ5lY1fLZJdWtHwDHDaQk0p46TLwrUsnVzQlWlQWfELO6xFIoUTen5JTD5zLjEVt7F3osaOFLlKZ7E9Fr6udU94QwJap3pkSIBs8OQYjhydIZbmOs8rycrffwxW5V_OG75u6AOdGQxtgtvHngEFXIpePXsCWV4olbUEExZITkRjj-PzFAVaCbyv6YIVWzpHu-YNt19m5L14sO5CoE_VLBp1NvXdgQsqJdfhcmZfrIEviqTpeFqvLBJrAFFrO73Em0wtvzyCY6roJkJ4Jex3B-T&client_secret=XXXXXXX

<- HTTP/1.1 400 Bad Request
<- WWW-Authenticate: OAuth "Facebook Platform" "invalid_code" "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request"
<- Access-Control-Allow-Origin: *
<- Pragma: no-cache
<- Cache-Control: no-store
<- facebook-api-version: v2.1
<- Expires: Sat, 01 Jan 2000 00:00:00 GMT
<- Content-Type: application/json; charset=UTF-8
<- x-fb-trace-id: EKfF0Ym5kiq
<- x-fb-rev: 2524340
<- Vary: Accept-Encoding
<- Content-Encoding: gzip
<- X-FB-Debug: edPjq3HBo6RBSnfLfu9r24il8l879O+t4pzgwC9L9QGDrGr8TUvShFAzoV3G4G1EshSR/tsJ9Vo0uACnXBSooQ==
<- Date: Wed, 24 Aug 2016 18:18:55 GMT
<- Connection: keep-alive
<- Content-Length: 183
<- 
Error in init_oauth2.0(self$endpoint, self$app, scope = self$params$scope,  : 
  Bad Request (HTTP 400). Failed to get an access token.

@tokumotion
Copy link

Found the fix.

Seems with the new httr (1.2.1) update you don't need to set Sys.setenv().

Ran this and worked:

library(httr)

oauth_endpoints("facebook")

app <- oauth_app('facebook', XXX, XXX)
fb_token <- oauth2.0_token(oauth_endpoints("facebook"), app,
                           scope = c("ads_management", "read_insights"),
                           type  = "application/x-www-form-urlencoded", 
                           cache = FALSE)
save(fb_token, file = "~/Documents/fb_token")

@daroczig
Copy link
Owner

@tokumotion the error message says "Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request" -- can you pls double check if you have the correct URL set on the UI, something like:

redirect url on fb

@daroczig
Copy link
Owner

Ah, OK, I'm glad you figured this out. Thanks for reporting back!

@tokumotion
Copy link

tokumotion commented Aug 24, 2016

You may need to update the package instructions for getting the token, as it currently includes the Sys.setenv()command. Thank you!
Correction: You already did!

@christerwi
Copy link

Note to all who still have problems with this:
I finally understood what @daroczig and others apparently knew before (issue closed corectly):

you can specify the redirect_uri in the oauth_app, I just finally made this work after manually getting tokens forever:

fb_token_oauth <- oauth2.0_token(oauth_endpoints("facebook"), app,
                           scope = c("ads_management", "read_insights"),
                           type  = "application/x-www-form-urlencoded", 
                           cache = FALSE)

note that for me the token landed in the name of an empty list; to extract, this will work:
fb_token <- fromJSON(names(fb_token_oauth$credentials[1]))$access_token

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants