I'm using Google API, specifically Youtube's Data API, to get comments above a video, using video ID. I'm using python example from Google Documentation here, but I've the following error:
Missing property "redirect_uris" in a client type of "web"
So, this is my client_secrets_....json file:
{
"installed": {
"client_id":"xxxxxxxxxxxx-xxxxje3a7fk4jbs63g6m1a2mand4sa3h.apps.googleusercontent.com",
"project_id":"progettotesicuozzo",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"XXXXXXXXXXX",
"redirect_uris":[
"urn:ietf:wg:oauth:2.0:oob",
"http://localhost"
]
}
}
This is the code:
# Usage example:
# python test.py --videoid='<video_id>' --text='<text>'
import httplib2
import os
import sys
import json
from apiclient.discovery import build_from_document
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
CLIENT_SECRETS_FILE = "./client_secret_....json"
YOUTUBE_READ_ONLY_SCOPE = "https://www.googleapis.com/auth/youtube.readonly"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
MISSING_CLIENT_SECRETS_MESSAGE = """WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file found at: %s with information from the APIs Console https://console.developers.google.com
For more information about the client_secrets.json file format, please visit: https://developers.google.com /api-client-library/python/guide/aaa_client_secrets""" % os.path.abspath(os.path.join(os.path.dirname(__file__), CLIENT_SECRETS_FILE))
def get_authenticated_service(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_ONLY_SCOPE, message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
with open("youtube-v3-discoverydocument.json", "r", encoding="utf8") as f:
doc = f.read()
return build_from_document(doc, http=credentials.authorize(httplib2.Http()))
def get_comment_threads(youtube, video_id):
results = youtube.commentThreads().list(
part="snippet",
videoId=video_id,
textFormat="plainText"
).execute()
for item in results["items"]:
comment = item["snippet"]["topLevelComment"]
author = comment["snippet"]["authorDisplayName"]
text = comment["snippet"]["textDisplay"]
print("Comment by " + author + ": " + text)
return results["items"]
def get_comments(youtube, parent_id):
results = youtube.comments().list(
part="snippet",
parentId=parent_id,
textFormat="plainText"
).execute()
for item in results["items"]:
author = item["snippet"]["authorDisplayName"]
text = item["snippet"]["textDisplay"]
print("Comment by " + author + ": " + text)
return results["items"]
if __name__ == '__main__':
# The "videoid" option specifies the YouTube video ID that uniquely
# identifies the video for which the comment will be inserted.
argparser.add_argument("--videoid", help="Required; ID for video for which the comment will be inserted.")
# The "text" option specifies the text that will be used as comment.
#argparser.add_argument("--text", help="Required; text that will be used as comment.")
args = argparser.parse_args()
if not args.videoid:
exit("Please specify videoid using the --videoid= parameter.")
youtube = get_authenticated_service(args)
# All the available methods are used in sequence just for the sake of an example.
try:
video_comment_threads = get_comment_threads(youtube, args.videoid)
parent_id = video_comment_threads[0]["id"]
video_comments = get_comments(youtube, parent_id)
except HttpError as e:
print("An HTTP error " + str(e.resp.status) + " occurred:\n" + json.dumps(str(e.content)))
else:
print("Inserted, listed, updated, moderated, marked and deleted comments.")
How can I configure well my json file, please?
After fixing my Json file I am now faced with this error:
An HTTP error 403 occurred:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
I've requested a my personal video, using the ID from the web URL, but I got the error, after I've been prompted to the redirect_uri.
When you create your project on google developer console you will be prompted to download the JSon file. You should not change this file you appear to have removed the redirect URI. Redirect URI is created as part of browser credentials. It is needed to tell the server where to return the response to.
Example:
{
"web":{
"client_id":"xxxxx-i6cjd1hkjntu5bkdkjj5cdnpcu4iju8p.apps.googleusercontent.com",
"project_id":"daimto-tutorials-101",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"xxxxx",
"redirect_uris":[
"http://localhost/google-api-php-client-samples/Analytics/Oauth2.php",
"http://localhost/google-api-php-client-samples/Calendar/Oauth2.php"
],
"javascript_origins":[
"https://www.example.com"
]
}
}
Authorized redirect URIs For use with requests from a web server. This
is the path in your application that users are redirected to after
they have authenticated with Google. The path will be appended with
the authorization code for access. Must have a protocol. Cannot
contain URL fragments or relative paths. Cannot be a public IP
address.
insufficientPermissions means that the user you are authenticating with does not have access to do what you are trying to do. You authenticated with a user and a channel. you only have access to the videos within that channel.
Related
I want to upload a video on YouTube that is not working. I do not know why
my code:
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def upload_video_to_youtube(video_file, title, description, tags):
try:
# Authenticate and build the YouTube API client
client_secrets_file = "client_secret.json"
scopes = ["https://www.googleapis.com/auth/youtube.upload"]
credentials = google.auth.default(scopes=scopes)
youtube = build(client_secrets_file,"youtube", "v3", credentials=credentials)
# Define the video metadata
request_body = {
"snippet": {
"title": title,
"description": description,
"tags": tags,
"categoryId": 22
},
"status": {
"privacyStatus": "private"
}
}
# Upload the video
with open(video_file, "rb") as video:
media = {"body": video}
response = youtube.videos().insert(
part=",".join(request_body.keys()),
body=request_body,
media_body=googleapiclient.http.MediaFileUpload(
video_file, chunksize=1024 * 1024, resumable=True
),
).execute()
print(f"Video uploaded with ID: {response['id']}")
except HttpError as error:
print(f"An error occured while uploading the video: {error}")
error:
Traceback (most recent call last): File
"c:\Users\Lukas\Dokumenty\python_scripts\Billionare
livestyle\main.py", line 142, in <module>
upload_video_to_youtube("video.mp4","test","test description","test") File
"c:\Users\Lukas\Dokumenty\python_scripts\Billionare
livestyle\main.py", line 90, in upload_video_to_youtube
credentials = google.auth.default(scopes=scopes) File "C:\Users\Lukas\Dokumenty\python_scripts\Billionare
livestyle\env\youtube\lib\site-packages\google\auth\_default.py", line
616, in default
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE) google.auth.exceptions.DefaultCredentialsError: Could not
automatically determine credentials. Please set
GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and
re-run the application. For more information, please see
https://cloud.google.com/docs/authentication/getting-started
You should be following the official Upload a Video example from the documentation. It shows you how to authorize your request.
#!/usr/bin/python
import httplib
import httplib2
import os
import random
import sys
import time
from apiclient.discovery import build
from apiclient.errors import HttpError
from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
# Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves.
httplib2.RETRIES = 1
# Maximum number of times to retry before giving up.
MAX_RETRIES = 10
# Always retry when these exceptions are raised.
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
httplib.IncompleteRead, httplib.ImproperConnectionState,
httplib.CannotSendRequest, httplib.CannotSendHeader,
httplib.ResponseNotReady, httplib.BadStatusLine)
# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the Google API Console at
# https://console.cloud.google.com/.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
# https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = "client_secrets.json"
# This OAuth 2.0 access scope allows an application to upload files to the
# authenticated user's YouTube channel, but doesn't allow other types of access.
YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube.upload"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the API Console
https://console.cloud.google.com/
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
CLIENT_SECRETS_FILE))
VALID_PRIVACY_STATUSES = ("public", "private", "unlisted")
def get_authenticated_service(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
scope=YOUTUBE_UPLOAD_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
def initialize_upload(youtube, options):
tags = None
if options.keywords:
tags = options.keywords.split(",")
body=dict(
snippet=dict(
title=options.title,
description=options.description,
tags=tags,
categoryId=options.category
),
status=dict(
privacyStatus=options.privacyStatus
)
)
# Call the API's videos.insert method to create and upload the video.
insert_request = youtube.videos().insert(
part=",".join(body.keys()),
body=body,
# The chunksize parameter specifies the size of each chunk of data, in
# bytes, that will be uploaded at a time. Set a higher value for
# reliable connections as fewer chunks lead to faster uploads. Set a lower
# value for better recovery on less reliable connections.
#
# Setting "chunksize" equal to -1 in the code below means that the entire
# file will be uploaded in a single HTTP request. (If the upload fails,
# it will still be retried where it left off.) This is usually a best
# practice, but if you're using Python older than 2.6 or if you're
# running on App Engine, you should set the chunksize to something like
# 1024 * 1024 (1 megabyte).
media_body=MediaFileUpload(options.file, chunksize=-1, resumable=True)
)
resumable_upload(insert_request)
# This method implements an exponential backoff strategy to resume a
# failed upload.
def resumable_upload(insert_request):
response = None
error = None
retry = 0
while response is None:
try:
print "Uploading file..."
status, response = insert_request.next_chunk()
if response is not None:
if 'id' in response:
print "Video id '%s' was successfully uploaded." % response['id']
else:
exit("The upload failed with an unexpected response: %s" % response)
except HttpError, e:
if e.resp.status in RETRIABLE_STATUS_CODES:
error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status,
e.content)
else:
raise
except RETRIABLE_EXCEPTIONS, e:
error = "A retriable error occurred: %s" % e
if error is not None:
print error
retry += 1
if retry > MAX_RETRIES:
exit("No longer attempting to retry.")
max_sleep = 2 ** retry
sleep_seconds = random.random() * max_sleep
print "Sleeping %f seconds and then retrying..." % sleep_seconds
time.sleep(sleep_seconds)
if __name__ == '__main__':
argparser.add_argument("--file", required=True, help="Video file to upload")
argparser.add_argument("--title", help="Video title", default="Test Title")
argparser.add_argument("--description", help="Video description",
default="Test Description")
argparser.add_argument("--category", default="22",
help="Numeric video category. " +
"See https://developers.google.com/youtube/v3/docs/videoCategories/list")
argparser.add_argument("--keywords", help="Video keywords, comma separated",
default="")
argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES,
default=VALID_PRIVACY_STATUSES[0], help="Video privacy status.")
args = argparser.parse_args()
if not os.path.exists(args.file):
exit("Please specify a valid file using the --file= parameter.")
youtube = get_authenticated_service(args)
try:
initialize_upload(youtube, args)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
I want to access AWS AppSync API using Python code and confused with requests library.
Auth mode is Cognito user pool. My questions are:
How to get access tokens from Cognito user pool?
How to make queries, mutations, and handle subscriptions?
I tried to do it with auth mode API key. but I am getting the following error.
import requests
import json
URL = "https://vtcarmq7zzeadnkwzcgfr24irm.appsync-api.us-east-1.amazonaws.com/graphql"
headers = {"x-api-key":"da2-bwuyzqchhfgyxemcmdinjegb7e"}
data = json.dumps({
"query": '''
listTodos(filter:{
title:{
contains:"g"
}
} ) {
items{
id title duedate
}
}
'''
} )
r = requests.request("POST", URL , data = data , headers = headers)
print(r.text)
{ "errors" : [ {
"message" : "Unable to parse GraphQL query.",
"errorType" : "MalformedHttpRequestException" } ] }
I have seen this video https://www.youtube.com/watch?v=2U4RsbFO4bA&t=1172s
In this video, for authentication using cognito user pool, he says to make a call to cognito user pool and get the tokens and pass it to aws appsync in headers.
I am new to aws and python request module, trying to write python code for this video.
graphql-python/gql supports AWS AppSync since version 3.0.0rc0.
It supports queries, mutation and even subscriptions on the realtime endpoint.
It supports IAM, api key and JWT authentication methods.
The documentation is available here
Here is an example of a mutation using the API Key authentication:
import asyncio
import os
import sys
from urllib.parse import urlparse
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from gql.transport.appsync_auth import AppSyncApiKeyAuthentication
# Uncomment the following lines to enable debug output
# import logging
# logging.basicConfig(level=logging.DEBUG)
async def main():
# Should look like:
# https://XXXXXXXXXXXXXXXXXXXXXXXXXX.appsync-api.REGION.amazonaws.com/graphql
url = os.environ.get("AWS_GRAPHQL_API_ENDPOINT")
api_key = os.environ.get("AWS_GRAPHQL_API_KEY")
if url is None or api_key is None:
print("Missing environment variables")
sys.exit()
# Extract host from url
host = str(urlparse(url).netloc)
auth = AppSyncApiKeyAuthentication(host=host, api_key=api_key)
transport = AIOHTTPTransport(url=url, auth=auth)
async with Client(
transport=transport, fetch_schema_from_transport=False,
) as session:
query = gql(
"""
mutation createMessage($message: String!) {
createMessage(input: {message: $message}) {
id
message
createdAt
}
}"""
)
variable_values = {"message": "Hello world!"}
result = await session.execute(query, variable_values=variable_values)
print(result)
asyncio.run(main())
I have tried uploading file to Google Drive from my local system using a Python script but I keep getting HttpError 403. The script is as follows:
from googleapiclient.http import MediaFileUpload
from googleapiclient import discovery
import httplib2
import auth
SCOPES = "https://www.googleapis.com/auth/drive"
CLIENT_SECRET_FILE = "client_secret.json"
APPLICATION_NAME = "test"
authInst = auth.auth(SCOPES, CLIENT_SECRET_FILE, APPLICATION_NAME)
credentials = authInst.getCredentials()
http = credentials.authorize(httplib2.Http())
drive_serivce = discovery.build('drive', 'v3', credentials=credentials)
file_metadata = {'name': 'gb1.png'}
media = MediaFileUpload('./gb.png',
mimetype='image/png')
file = drive_serivce.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print('File ID: %s' % file.get('id'))
The error is :
googleapiclient.errors.HttpError: <HttpError 403 when requesting
https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&alt=json&fields=id
returned "Insufficient Permission: Request had insufficient authentication scopes.">
Am I using the right scope in the code or missing anything ?
I also tried a script I found online and it is working fine but the issue is that it takes a static token, which expires after some time. So how can I refresh the token dynamically?
Here is my code:
import json
import requests
headers = {
"Authorization": "Bearer TOKEN"}
para = {
"name": "account.csv",
"parents": ["FOLDER_ID"]
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': ('mimeType', open("./test.csv", "rb"))
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(r.text)
Answer:
Delete your token.pickle file and re-run your application.
More Information:
As long as you have the correct set of credentials then all that is required when you update the scopes of your application is to re-obtain a token. Delete the token file located in the application's root folder, then run the application again. If you have the https://www.googleapis.com/auth/drive scope, and the Gmail API enabled in the developer console, you should be good.
References:
Google Drive API - Files: create method
"Insufficient Permission: Request had insufficient authentication scopes."
Means that the user you have authenticated with has not granted your application permission to do what you are trying to do.
The files.create method requires that you have authenticated the user with one of the following scopes.
while your code does appear to be using the full on drive scope. What i suspect has happens is that you have authenticated your user then changed the scope in your code and not promoted the user to login again and grant consent. You need to remove the users consent from your app either by having them remove it directly in their google account or just deleteing the credeitnals you have stored in your app. This will force the user to login again.
There is also an approval prompt force option to the google login but am not a python dev so im not exactly sure how to force that. it should be something like the prompt='consent' line below.
flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope='https://spreadsheets.google.com/feeds '+
'https://docs.google.com/feeds',
redirect_uri='http://example.com/auth_return',
prompt='consent')
consent screen
If done correctly the user should see a screen like this
Prompting them to grant you full access to their drive account
Token pickle
If you are following googles tutorial here https://developers.google.com/drive/api/v3/quickstart/python you need to delete the token.pickle that contains the users stored consent.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
You can use the google-api-python-client to build a Drive service for using Drive API.
Get your Authorization as by following the first 10 steps of this answer.
If you want the user to go through consent screen only once, then store the credentials in a file. They include a refresh token that app can use to request authorization after expired.Example
With a valid Drive Service you can upload a file by calling a function like the following upload_file:
def upload_file(drive_service, filename, mimetype, upload_filename, resumable=True, chunksize=262144):
media = MediaFileUpload(filename, mimetype=mimetype, resumable=resumable, chunksize=chunksize)
# Add all the writable properties you want the file to have in the body!
body = {"name": upload_filename}
request = drive_service.files().create(body=body, media_body=media).execute()
if getFileByteSize(filename) > chunksize:
response = None
while response is None:
chunk = request.next_chunk()
if chunk:
status, response = chunk
if status:
print("Uploaded %d%%." % int(status.progress() * 100))
print("Upload Complete!")
Now pass in the parameters and call the function...
# Upload file
upload_file(drive_service, 'my_local_image.png', 'image/png', 'my_imageination.png' )
You will see the file with the name: my_imageination.png in your Google Drive root folder.
More about the Drive API v3 service and available methods here.
getFileSize() function:
def getFileByteSize(filename):
# Get file size in python
from os import stat
file_stats = stat(filename)
print('File Size in Bytes is {}'.format(file_stats.st_size))
return file_stats.st_size
Uploading to certain folder(s) in your drive is easy...
Just add the parent folder Id(s) in the body of the request.
Here are the properties of a File.
Example:
request_body = {
"name": "getting_creative_now.png",
"parents": ['myFiRsTPaRentFolderId',
'MyOtherParentId',
'IcanTgetEnoughParentsId'],
}
To use the scope 'https://www.googleapis.com/auth/drive' you need to submit the google app for verification.
Find the image for scope
So use the scope 'https://www.googleapis.com/auth/drive.file' instead of 'https://www.googleapis.com/auth/drive' to upload files without verification.
Also use SCOPES as list.
ex: SCOPES = ['https://www.googleapis.com/auth/drive.file']
I can successfully upload and download the files to google drive by using the above SCOPE.
I found the solution for uploading a file to google drive. Here it is:
import requests
import json
url = "https://www.googleapis.com/oauth2/v4/token"
payload = "{\n\"" \
"client_id\": \"CLIENT_ID" \
"\",\n\"" \
"client_secret\": \"CLIENT SECRET" \
"\",\n\"" \
"refresh_token\": \"REFRESH TOKEN" \
"\",\n\"" \
"grant_type\": \"refresh_token\"\n" \
"}"
headers = {
'grant_type': 'authorization_code',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
res = json.loads(response.text.encode('utf8'))
headers = {
"Authorization": "Bearer %s" % res['access_token']
}
para = {
"name": "file_path",
"parents": "google_drive_folder_id"
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
# 'file': open("./gb.png", "rb")
'file': ('mimeType', open("file_path", "rb"))
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(r.text)
For generating client id, client secret and refresh token, you can follow the link :- click here
Maybe the question is a little bit outdated, but I found an easy way to upload files on google drive from python
pip install gdrive-python
Then you have to allow the script to upload files on your Google account with this command and follow the instructions:
python -m drive about
Finally, upload the file:
form gdrive import GDrive
drive = GDrive()
drive.upload('path/to/file')
More info on the GitHub repo: https://github.com/vittoriopippi/gdrive-python
I am using the sample code in this site https://developers.google.com/youtube/reporting/v1/reference/rest/v1/reportTypes/list
After setting-up successfully libraries and importing CLIENT_SECRETS_FILE into the code. When I run the code. It returned
Report type id: playlist_playback_location_a1
name: Playlist playback locations
Report type id: playlist_province_a1
name: Playlist province
Report type id: playlist_traffic_source_a1
name: Playlist traffic sources
I want to get playlist users in playlist_basic_a1 table so I entered the reportTypeId for the job: playlist_basic_a1
and set a name for the job: Playlist user activity
Then It returned an error like this:
An HTTP error 409 occurred:
{
"error": {
"code": 409,
"message": "Requested entity already exists",
"errors": [
{
"message": "Requested entity already exists",
"domain": "global",
"reason": "alreadyExists"
}
],
"status": "ALREADY_EXISTS"
}
}
This is my code:
#!/usr/bin/python
# Usage example:
# python create_reporting_job.py --name='<name>'
import httplib2
import os
import sys
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the Google API Console at
# https://console.developers.google.com/.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
# https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
# https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = "client_secrets.json"
# This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for
# authenticated user's account. Any request that retrieves earnings or ad performance metrics must
# use this scope.
YOUTUBE_ANALYTICS_MONETARY_READ_SCOPE = (
"https://www.googleapis.com/auth/yt-analytics-monetary.readonly")
YOUTUBE_REPORTING_API_SERVICE_NAME = "youtubereporting"
YOUTUBE_REPORTING_API_VERSION = "v1"
# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:
%s
with information from the APIs Console
https://console.developers.google.com
For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
CLIENT_SECRETS_FILE))
# Authorize the request and store authorization credentials.
def get_authenticated_service(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_ANALYTICS_MONETARY_READ_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
return build(YOUTUBE_REPORTING_API_SERVICE_NAME, YOUTUBE_REPORTING_API_VERSION,
http=credentials.authorize(httplib2.Http()))
# Call the YouTube Reporting API's reportTypes.list method to retrieve report types.
def list_report_types(youtube_reporting):
results = youtube_reporting.reportTypes().list().execute()
reportTypes = results["reportTypes"]
if "reportTypes" in results and results["reportTypes"]:
reportTypes = results["reportTypes"]
for reportType in reportTypes:
print "Report type id: %s\n name: %s\n" % (reportType["id"], reportType["name"])
else:
print "No report types found"
return False
return True
# Call the YouTube Reporting API's jobs.create method to create a job.
def create_reporting_job(youtube_reporting, report_type_id, name):
reporting_job = youtube_reporting.jobs().create(
body=dict(
reportTypeId=report_type_id,
name=name
)
).execute()
print ("Reporting job '%s' created for reporting type '%s' at '%s'"
% (reporting_job["name"], reporting_job["reportTypeId"],
reporting_job["createTime"]))
# Prompt the user to enter a report type id for the job. Then return the id.
def get_report_type_id_from_user():
report_type_id = raw_input("Please enter the reportTypeId for the job: ")
print ("You chose '%s' as the report type Id for the job." % report_type_id)
return report_type_id
if __name__ == "__main__":
# The "name" option specifies the name that will be used for the reporting job.
argparser.add_argument("--name",
help="Required; name for the reporting job.")
args = argparser.parse_args()
if not args.name:
exit("Please specify name using the --name= parameter.")
youtube_reporting = get_authenticated_service(args)
try:
if list_report_types(youtube_reporting):
create_reporting_job(youtube_reporting, get_report_type_id_from_user(), args.name)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
else:
print "Created reporting job."
Why I got this error? And how to fix this error
I just tested your code and... It's working completely fine.
The job name must be unique for within a reportTypeId, so that error means that you used the name Playlist user activity for another job of the same type already.
That is not clear in the documentation. You can open an issue about the docs on their issue tracker here
I'm trying to create Circles with the Google+ API, but I'm kinda stuck, this is my code, it was more or less copied from the official API documentation (yes I know it doesn't create Circle, but the issue is the same)
import httplib2
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
import json
with open('client_secrets.json', 'r') as f:
json_data = json.load(f)
data = json_data['web']
CLIENT_ID = data['client_id']
CLIENT_SECRET = data['client_secret']
# List the scopes your app requires:
SCOPES = ['https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/plus.circles.write']
# The following redirect URI causes Google to return a code to the user's
# browser that they then manually provide to your app to complete the
# OAuth flow.
REDIRECT_URI = 'http://localhost/oauth2callback'
# For a breakdown of OAuth for Python, see
# https://developers.google.com/api-client-library/python/guide/aaa_oauth
# CLIENT_ID and CLIENT_SECRET come from your APIs Console project
flow = OAuth2WebServerFlow(client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
scope=SCOPES,
redirect_uri=REDIRECT_URI)
auth_uri = flow.step1_get_authorize_url()
# This command-line server-side flow example requires the user to open the
# authentication URL in their browser to complete the process. In most
# cases, your app will use a browser-based server-side flow and your
# user will not need to copy and paste the authorization code. In this
# type of app, you would be able to skip the next 3 lines.
# You can also look at the client-side and one-time-code flows for other
# options at https://developers.google.com/+/web/signin/
print 'Please paste this URL in your browser to authenticate this program.'
print auth_uri
code = raw_input('Enter the code it gives you here: ')
# Set authorized credentials
credentials = flow.step2_exchange(code)
# Create a new authorized API client.
http = httplib2.Http()
http = credentials.authorize(http)
service = build('plusDomains', 'v1', http=http)
from apiclient import errors
try:
people_service = service.people()
people_document = people_service.get(userId='me').execute()
except errors.HttpError, e:
print e.content
My output:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Forbidden"
}
],
"code": 403,
"message": "Forbidden"
}
}
I searched for answer, but didn't really find any. On the API console I have Google+ API and
Google+ Domains API services added also my secret and client id are okay (otherwise the whole script would fail sooner). Also the auth is successful, my app's name is shown under https://accounts.google.com/IssuedAuthSubTokens. What did I miss?
The problem lies with your REDIRECT_URI variable. When you are using OAuth 2.0 in a purely server-side flow, the redirect URI MUST be 'urn:ietf:wg:oauth:2.0:oob'.
Try changing the variable like so (and be sure to update your client ID in the API Console):
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
Edit: Also, make sure that you are making your API call for a user within a domain. The Google+ Domains API only permits API calls that are restricted to users and content within that domain.