0

I have this curl GET Request, which afterwards I use curlconverter to convert it to a httr call in R.

curl -v -H "Accept: application/json" -H "User-Agent: User-Agent" -H "Authorization: Bearer {accesstoken}" https://host/query/api_endpoint

However, the token I receive from the application has key value pair structure that contains the accesstoken which is working, expiry, and a refresh token. After some time, the accesstoken expires. How do I capture refresh token in cURL so that everytime I do't have to request a new one?

andy
  • 1,947
  • 5
  • 27
  • 46

1 Answers1

0

Since I had the same problem and one suggestion was to do it with Python, following a solution in Python using the requests library. Please follow this answer to create a working refresh token first.

In my example, I always get a new access_token using the refresh_token to add a video to a private playlist:

import requests
import json

# according to  https://stackoverflow.com/a/41556775/3774227
client_id = '<client_id>'
client_secret = '<client_secret>'
refresh_token = '<refresh_token>'

playlist_id = '<playlist>'
video_id = 'M7FIvfx5J10'


def get_access_token(client_id, client_secret, refresh_token):

    url = 'https://www.googleapis.com/oauth2/v4/token'

    data = {
        'client_id': client_id,
        'client_secret': client_secret,
        'refresh_token': refresh_token,
        'grant_type': 'refresh_token'
    }

    response = requests.post(
        url=url,
        data=data,
    )

    return response.json().get('access_token')


def add_video_to_playlist(playlist_id, video_id, access_token):

    url = 'https://www.googleapis.com/youtube/v3/playlistItems'

    params = {
        'part': 'snippet',
    }

    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {}'.format(access_token)
    }

    data = {
        'snippet': {
            'playlistId': playlist_id,
            'resourceId': {
                'kind': 'youtube#video',
                'videoId': video_id
            },
        }
    }

    requests.post(
        url=url,
        params=params,
        headers=headers,
        data=json.dumps(data)
    )


if __name__ == '__main__':
    access_token = get_access_token(client_id, client_secret, refresh_token)
    add_video_to_playlist(playlist_id, video_id, access_token)
Livioso
  • 1,242
  • 1
  • 11
  • 21