{"action":"created","issue":{"url":"https://api.github.com/repos/googlesamples/apps-script-oauth2/issues/51","repository_url":"https://api.github.com/repos/googlesamples/apps-script-oauth2","labels_url":"https://api.github.com/repos/googlesamples/apps-script-oauth2/issues/51/labels{/name}","comments_url":"https://api.github.com/repos/googlesamples/apps-script-oauth2/issues/51/comments","events_url":"https://api.github.com/repos/googlesamples/apps-script-oauth2/issues/51/events","html_url":"https://github.com/googlesamples/apps-script-oauth2/issues/51","id":187173966,"number":51,"title":"Token expires when I call OAuth in an external library","user":{"login":"FirePanther","id":1711106,"avatar_url":"https://avatars.githubusercontent.com/u/1711106?v=3","gravatar_id":"","url":"https://api.github.com/users/FirePanther","html_url":"https://github.com/FirePanther","followers_url":"https://api.github.com/users/FirePanther/followers","following_url":"https://api.github.com/users/FirePanther/following{/other_user}","gists_url":"https://api.github.com/users/FirePanther/gists{/gist_id}","starred_url":"https://api.github.com/users/FirePanther/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/FirePanther/subscriptions","organizations_url":"https://api.github.com/users/FirePanther/orgs","repos_url":"https://api.github.com/users/FirePanther/repos","events_url":"https://api.github.com/users/FirePanther/events{/privacy}","received_events_url":"https://api.github.com/users/FirePanther/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-11-03T19:57:55Z","updated_at":"2016-11-04T20:45:51Z","closed_at":"2016-11-04T20:45:51Z","body":"I have a local gs script with the functions 'run' and 'authCallbackTest' and an external gs script with the OAuth itself (OAuth2.createService...).\r\nThe local gs script includes the external gs script via 'Libraries...'.\r\nThe token will be created successfully (with param access_type 'offline') but expires after an hour.\r\nWhen I put the external code (the createService part) into the internal code it can be refreshed without expiration.\r\n\r\nscript 1 (internal)\r\n```javascript\r\n// token generated: 19:53\r\n// expired in 20:53, just tested\r\n\r\nfunction run() {\r\n  var service = test.getContactsService(opts());\r\n  if (!service.hasAccess()) {\r\n    var authorizationUrl = service.getAuthorizationUrl();\r\n    Logger.log('open link:');\r\n    Logger.log(authorizationUrl);\r\n    throw 'Expired';\r\n  } else {\r\n    Logger.log('Everything is fine');\r\n  }\r\n}\r\n\r\nfunction authCallbackTest(request) {\r\n\tvar service = test.getContactsService(opts());\r\n\ttry {\r\n\t\tif (service.handleCallback(request)) return HtmlService.createHtmlOutput('success');\r\n\t\telse return HtmlService.createHtmlOutput('denied');\r\n\t} catch(e) {\r\n\t\treturn HtmlService.createHtmlOutput(e);\r\n\t}\r\n}\r\n\r\nfunction opts() {\r\n  return {\r\n    clientId: '...',\r\n    clientSecret: '...'\r\n  };\r\n}\r\n```\r\n\r\nscript 2 \"test\" (external)\r\n```javascript\r\nfunction getContactsService(opt) {\r\n\topt = opt || this.options;\r\n\treturn OAuth2.createService('contacts')\r\n\t\t.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')\r\n\t\t.setTokenUrl('https://accounts.google.com/o/oauth2/token')\r\n\t\t.setClientId(opt.clientId)\r\n\t\t.setClientSecret(opt.clientSecret)\r\n\t\t.setCallbackFunction('authCallbackTest')\r\n\t\t.setPropertyStore(PropertiesService.getUserProperties())\r\n\t\t.setScope('https://www.google.com/m8/feeds')\r\n\t\t.setParam('login_hint', Session.getActiveUser().getEmail())\r\n\t\t.setParam('access_type', 'offline')\r\n\t\t.setParam('approval_prompt', 'auto');\r\n}\r\n```"},"comment":{"url":"https://api.github.com/repos/googlesamples/apps-script-oauth2/issues/comments/258542985","html_url":"https://github.com/googlesamples/apps-script-oauth2/issues/51#issuecomment-258542985","issue_url":"https://api.github.com/repos/googlesamples/apps-script-oauth2/issues/51","id":258542985,"user":{"login":"erickoledadevrel","id":991384,"avatar_url":"https://avatars.githubusercontent.com/u/991384?v=3","gravatar_id":"","url":"https://api.github.com/users/erickoledadevrel","html_url":"https://github.com/erickoledadevrel","followers_url":"https://api.github.com/users/erickoledadevrel/followers","following_url":"https://api.github.com/users/erickoledadevrel/following{/other_user}","gists_url":"https://api.github.com/users/erickoledadevrel/gists{/gist_id}","starred_url":"https://api.github.com/users/erickoledadevrel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/erickoledadevrel/subscriptions","organizations_url":"https://api.github.com/users/erickoledadevrel/orgs","repos_url":"https://api.github.com/users/erickoledadevrel/repos","events_url":"https://api.github.com/users/erickoledadevrel/events{/privacy}","received_events_url":"https://api.github.com/users/erickoledadevrel/received_events","type":"User","site_admin":false},"created_at":"2016-11-04T20:45:51Z","updated_at":"2016-11-04T20:45:51Z","body":"My guess is that this is due to a missing refresh_token. The refresh_token is only returned when the user saw the dialog, which happens the first time they approve that application or if you set approval_prompt to \"force\". Try switching to \"force\" and I think you should get a refresh_token."}}
{"actor":{"display_login":"erickoledadevrel"}}