Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1.  
  2. /**
  3. * This script allows the stepping through of the Authorization Code Grant in
  4. * order to obtain a refresh token.
  5. *
  6. * This script uses the out-of-band redirect URI, which is not part of the
  7. * OAuth2 standard, to allow not redirecting the user. If this does not work
  8. * with your API, try instead the OAuth playground:
  9. * https://developers.google.com/oauthplayground/
  10. *
  11. * Execute script twice:
  12. * Execution 1: will result in a URL, which when placed in the browser will
  13. * issue a code.
  14. * Execution 2: place the code in "CODE" below and execute. If successful a
  15. * refresh token will be printed to the console.
  16. */
  17. var CLIENT_ID = '55177037730-0u9fc0avh8o5enk6c2027lgduitsmhg1.apps.googleusercontent.com';
  18. var CLIENT_SECRET = 'iATBj5i5XGo67kqZXEl4ur3I';
  19.  
  20. // Enter required scopes, e.g. ['https://www.googleapis.com/auth/drive']
  21. var SCOPES = ['ENTER_SCOPES'];
  22.  
  23. // Auth URL, e.g. https://accounts.google.com/o/oauth2/auth
  24. var AUTH_URL = 'https://accounts.google.com/o/oauth2/v2/auth';
  25. // Token URL, e.g. https://accounts.google.com/o/oauth2/token
  26. var TOKEN_URL = 'www.googleapis.com/oauth2/v4/token';
  27.  
  28. // After execution 1, enter the OAuth code inside the quotes below:
  29. var CODE = 'code=4%2FlwA707dWP5AiJS8BKYb_IJjcKpQwq7eaGOsaVjh5FKqrPVHt2GK6-NfCzTMK9XmcCCOY7CJzR2ofefsCnsmYJ1k&redirect_uri=https%3A%2F%2Fdevelopers.google.com%2Foauthplayground&client_id=55177037730-0u9fc0avh8o5enk6c2027lgduitsmhg1.apps.googleusercontent.com&client_secret=iATBj5i5XGo67kqZXEl4ur3I&scope=&grant_type=authorization_code';
  30.  
  31. function main() {
  32. if (CODE) {
  33. console.log("token");
  34. generateRefreshToken();
  35. } else {
  36. generateAuthUrl();
  37. }
  38. }
  39. main()
  40. /**
  41. * Creates the URL for pasting in the browser, which will generate the code
  42. * to be placed in the CODE variable.
  43. */
  44. function generateAuthUrl() {
  45. var payload = {
  46. scope: SCOPES.join(' '),
  47. // Specify that no redirection should take place
  48. // This is Google-specific and not part of the OAuth2 specification.
  49. redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
  50. response_type: 'code',
  51. access_type: 'offline',
  52. client_id: CLIENT_ID
  53. };
  54. var options = {payload: payload};
  55. var request = UrlFetchApp.getRequest(AUTH_URL, options);
  56. Logger.log(
  57. 'Browse to the following URL: ' + AUTH_URL + '?' + request.payload);
  58. }
  59.  
  60. /**
  61. * Generates a refresh token given the authorization code.
  62. */
  63. async function generateRefreshToken() {
  64. var payload = {
  65. code: CODE,
  66. client_id: CLIENT_ID,
  67. client_secret: CLIENT_SECRET,
  68. // Specify that no redirection should take place
  69. // This is Google-specific and not part of the OAuth2 specification.
  70. redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
  71. grant_type: 'authorization_code'
  72. };
  73.  
  74. //console.log(response);
  75. var options = {method: 'POST', payload: payload};
  76. var response = UrlFetchApp.fetch(TOKEN_URL, options);
  77. var data = JSON.parse(response.getContentText());
  78. if (data.refresh_token) {
  79. var msg = 'Success! Refresh token: ' + data.refresh_token +
  80. '\n\nThe following may also be a useful format for pasting into your ' +
  81. 'script:\n\n' +
  82. 'var CLIENT_ID = \'' + CLIENT_ID + '\';\n' +
  83. 'var CLIENT_SECRET = \'' + CLIENT_SECRET + '\';\n' +
  84. 'var REFRESH_TOKEN = \'' + data.refresh_token + '\';';
  85. Logger.log(msg);
  86. } else {
  87. Logger.log(
  88. 'Error, failed to generate Refresh token: ' +
  89. response.getContentText());
  90. }
  91. console.log(data);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement