{"action":"created","issue":{"url":"https://api.github.com/repos/MarkEdmondson1234/googleAuthR/issues/50","repository_url":"https://api.github.com/repos/MarkEdmondson1234/googleAuthR","labels_url":"https://api.github.com/repos/MarkEdmondson1234/googleAuthR/issues/50/labels{/name}","comments_url":"https://api.github.com/repos/MarkEdmondson1234/googleAuthR/issues/50/comments","events_url":"https://api.github.com/repos/MarkEdmondson1234/googleAuthR/issues/50/events","html_url":"https://github.com/MarkEdmondson1234/googleAuthR/issues/50","id":182227289,"number":50,"title":".httr-oauth file expiration..?","user":{"login":"SimonHasager","id":16938645,"avatar_url":"https://avatars.githubusercontent.com/u/16938645?v=3","gravatar_id":"","url":"https://api.github.com/users/SimonHasager","html_url":"https://github.com/SimonHasager","followers_url":"https://api.github.com/users/SimonHasager/followers","following_url":"https://api.github.com/users/SimonHasager/following{/other_user}","gists_url":"https://api.github.com/users/SimonHasager/gists{/gist_id}","starred_url":"https://api.github.com/users/SimonHasager/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SimonHasager/subscriptions","organizations_url":"https://api.github.com/users/SimonHasager/orgs","repos_url":"https://api.github.com/users/SimonHasager/repos","events_url":"https://api.github.com/users/SimonHasager/events{/privacy}","received_events_url":"https://api.github.com/users/SimonHasager/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-11T10:19:37Z","updated_at":"2016-10-11T17:50:02Z","closed_at":null,"body":"Hi Mark,\r\n\r\nLong time no see. I've started to use your amazing packages quite a lot. And it's been a pleasure! However, I've faced an issue. It's seems like the .httr-oauth file created with `gar_auth()` expires after one hour, which means that I can't create automated daily extracts of GA data :-(\r\n\r\nEverything works just fine when I run my R scripts (both in RStudio and the Command Prompt) within one hour after creating the .httr-oauth file. But after one hour (or the next day) I get this error:\r\n\r\n```\r\nWalking through data.\r\nRequest to profileId:  ()\r\nError in f(content, ...) : Invalid Credentials\r\n```\r\n\r\nThen, if I delete the .httr-oauth file (and thereby force it to create a new file) or run `gar_auth(new_user=TRUE)`, it will all work again.\r\n\r\nCan the .httr-oauth file expire? Or is it just me being a rookie? I hope you can help me.\r\n\r\nHere is some sample code:\r\n\r\n```\r\n## Set working directory\r\nsetwd(\"my/project/folder/path\")\r\n\r\n## Load libraries (I have installed the most recent CRAN versions)\r\nlibrary(googleAuthR)\r\nlibrary(googleAnalyticsR)\r\n\r\n## My googleAuthR client\r\noptions(\"googleAuthR.client_id\" = \"my.authR.client.id\")\r\noptions(\"googleAuthR.client_secret\" = \"my_authR_client_secret\")\r\noptions(\"googleAuthR.scopes.selected\" = \"https://www.googleapis.com/auth/analytics.readonly\")\r\n\r\n## My googleAnalyticsR client\r\noptions(\"googleAnalyticsR.client_id\" = \"my.analyticsR.client.id\")\r\noptions(\"googleAnalyticsR.client_secret\" = \"my.analyticsR.client.secret\")\r\n\r\n## Both clients have the authorized redirect URI set to http://localhost:1410/\r\n## I have tried using the default googleAuthR client, but the issue is the same\r\n\r\n## Set GA view ID\r\nviewID <- 123456789\r\n\r\n## Read (or create) .httr-oauth file\r\ngar_auth()\r\n\r\n## Set date range (to yesterday)\r\nstart <- as.character(Sys.Date() - 1)\r\nend <- as.character(Sys.Date() - 1)\r\n\r\n## Fetch some data\r\nga <- google_analytics(\r\n  viewID,\r\n  start = start,\r\n  end = end,\r\n  metrics = c(\"sessions\", \"bounces\"),\r\n  dimensions = c(\"date\", \"channelGrouping\"),\r\n  samplingLevel = \"WALK\",\r\n  max = 999999999\r\n)\r\n```"},"comment":{"url":"https://api.github.com/repos/MarkEdmondson1234/googleAuthR/issues/comments/252992343","html_url":"https://github.com/MarkEdmondson1234/googleAuthR/issues/50#issuecomment-252992343","issue_url":"https://api.github.com/repos/MarkEdmondson1234/googleAuthR/issues/50","id":252992343,"user":{"login":"MarkEdmondson1234","id":3155884,"avatar_url":"https://avatars.githubusercontent.com/u/3155884?v=3","gravatar_id":"","url":"https://api.github.com/users/MarkEdmondson1234","html_url":"https://github.com/MarkEdmondson1234","followers_url":"https://api.github.com/users/MarkEdmondson1234/followers","following_url":"https://api.github.com/users/MarkEdmondson1234/following{/other_user}","gists_url":"https://api.github.com/users/MarkEdmondson1234/gists{/gist_id}","starred_url":"https://api.github.com/users/MarkEdmondson1234/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MarkEdmondson1234/subscriptions","organizations_url":"https://api.github.com/users/MarkEdmondson1234/orgs","repos_url":"https://api.github.com/users/MarkEdmondson1234/repos","events_url":"https://api.github.com/users/MarkEdmondson1234/events{/privacy}","received_events_url":"https://api.github.com/users/MarkEdmondson1234/received_events","type":"User","site_admin":false},"created_at":"2016-10-11T17:50:02Z","updated_at":"2016-10-11T17:50:02Z","body":"Hi Simon, hope alls well :)\r\n\r\nYes the prime function of the httr token is to be used to refresh after the current authentication is over, so something is going wrong when it tries to refresh.\r\n\r\nIt sometimes happens if the client_id/secret you authenticated with is different from the one you are refreshing.  It looks like you are setting a lot of client Ids there, so I would remove some of those - it shouldn't be necessary to set them in googleAuthR as well as googleAnalyticsR.\r\n\r\nStart first with the default project of googleAnalyticsR and once working you have something to go back to when you try your own project.  The auth keys need to be set exactly the same when authenticating first time and subsequent times, which is set when you load the library.   If setting your own client Ids, it needs to be after loading the library but before you call ga_auth() (note not gar_auth() )\r\n\r\nYou also should not be setting the redirect to port 1410, thats taken care of by httr itself.\r\n\r\nIf all else fails, try using the gar_auth_service() function instead, which requires you download the JSON file.  That doens't need refreshing and is recommended for automated scripts, but you will need to add the service account email (blablahblah@project.google.com) as a Viewer to the GA View you are downloading from.   You can then set that in an environment variable so you get auto-authenticated when you load the library (when using the github version - see README)\r\n\r\nSo, modifying your code:\r\n\r\n```r\r\n## Set working directory\r\nsetwd(\"my/project/folder/path\")\r\n\r\n## Just load this one\r\nlibrary(googleAnalyticsR)\r\n\r\n## My googleAnalyticsR client\r\n## try without this first.\r\n##options(\"googleAnalyticsR.client_id\" = \"my.analyticsR.client.id\")\r\n##options(\"googleAnalyticsR.client_secret\" = \"my.analyticsR.client.secret\")\r\n\r\n## Set GA view ID\r\nviewID <- 123456789\r\n\r\n## Read (or create) .httr-oauth file\r\nga_auth()\r\n\r\n## or googleAuthR::gar_auth_service(\"json_file.json\")\r\n\r\n## Set date range (to yesterday)\r\nstart <- as.character(Sys.Date() - 1)\r\nend <- as.character(Sys.Date() - 1)\r\n\r\n## Fetch some data\r\nga <- google_analytics(\r\n  viewID,\r\n  start = start,\r\n  end = end,\r\n  metrics = c(\"sessions\", \"bounces\"),\r\n  dimensions = c(\"date\", \"channelGrouping\"),\r\n  samplingLevel = \"WALK\",\r\n  max = 999999999\r\n)\r\n```\r\n\r\nGood luck! Hope it helps. "}}
{"actor":{"display_login":"MarkEdmondson1234"}}