I'm trying to get history feed from YouTube of an authenticated user with python.
This is my code :
yt_service = gdata.youtube.service.YouTubeService()
def LogIn():
login_name = raw_input('Email:')
login_pass = getpass.getpass()
try:
yt_service.email = login_name
yt_service.password = login_pass
yt_service.ProgrammaticLogin()
except:
print 'False username or password. Unable to authenticate.'
exit();
def GetHistoryFeed():
uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_history?v=2'
feed = yt_service.GetYouTubeVideoFeed(uri)
#PrintVideoFeed(yt_service.GetYouTubeVideoFeed(uri),'history')
LogIn()
GetHistoryFeed()
and it says gdata.service.RequestError: {'status': 400, 'body': 'Invalid request URI', 'reason': 'Bad Request'} . I know I have to make a authenticated Get request , but i don't know how. What am I doing wrong ?
EDIT
I am facing a major problem. The prog is the same as above , but with yt_service.developer_key = DEVELOPER_KEY added under password line and uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_history?v=2&key=%s'%DEVELOPER_KEY. I tested it in 4 PCs and it runs without errors only in one if them. I get this error :
File "/usr/local/lib/python2.6/dist-packages/gdata/youtube/service.py", line 186, in
return self.Get(uri, converter=gdata.youtube.YouTubeVideoFeedFromString)
File "/usr/local/lib/python2.6/dist-packages/gdata/service.py", line 1108, in Get
'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 400, 'body': 'Invalid request URI', 'reason': 'Bad Request'}
I use python 2.7 and gdata python 2.0 . Why one Pc executes it and the rest of them not? What can i do to fix it ? Please help!
When you attempt to call youtube API, you will first need to register a new application. Reference - https://developers.google.com/youtube/2.0/developers_guide_protocol_authentication
Visit http://code.google.com/apis/youtube/dashboard/ to register your application and retrieve the Developer Key that will be generated for you.
Thereafter, whenever you make a call to youtube APIs, you should include the key query parameter. (reference - https://developers.google.com/youtube/2.0/developers_guide_protocol#Developer_Key)
Your instantiated yt_service will be:-
yt_service.developer_key = DEVELOPER_KEY
where the DEVELOPER_KEY is the one that you get on your newly registered application's dashboard ( http://code.google.com/apis/youtube/dashboard/ ).
Without this DEVELOPER_KEY, google youtube will not know whether your python script is in fact a recognized application, with proper access rights.
Related
I am trying to access Uber API for requesting rides and I am trying to access appropriate scope like
profile
request_rides
request
And I successfully got the token, clientid, clientsecret, access_token and Then I tried to access profile by calling
from uber_rides.auth import AuthorizationCodeGrant
auth_flow = AuthorizationCodeGrant(
<CLIENT_ID>,
<SCOPES>,
<CLIENT_SECRET>,
<REDIRECT_URI>
)
auth_url = auth_flow.get_authorization_url()
session = auth_flow.get_session(redirect_url)
client = UberRidesClient(session, sandbox_mode=True)
credentials = session.oauth2credential
Above commands didn't raise any errors but when I execute
response = client.get_user_profile()
then I showed
uber_rides.errors.ClientError: 401: This endpoint requires at least one of the following scopes: profile, eats.pos_provisioning, profile.internal_uuid
And When I append profile in below <SCOPES> in browser then It showed invalid_scope . I have no idea why it is showing this error every time I call the url to get the session.
In https://login.uber.com/oauth/v2/token?clientid= POST request, it is showing only "scope": "offline_access" in scope.
So How can I get the access of profile and request_rides scope ?
Any help would be much Appreciated.
I was trying to get access token to create other API Calls. I already got the authorization code from sellers account but for some reason my python script output says:
{'code': 'InvalidCode', 'type': 'ISP', 'message': 'Invalid authorization code', 'request_id': '2102ebef16741206553665'}
This is the python script I used from their SDK:
import lazop_sdk
client = lazop_sdk.LazopClient("https://auth.lazada.com/rest", appKey, appSecret)
request = lazop_sdk.LazopRequest("/auth/token/create")
request.add_api_param("code", authorizationCode)
request.add_api_param("uuid", "This field is currently invalid, do not use this field please")
response = client.execute(request)
print(response.type)
print(response.body)
Dis I miss anything? I'm pretty sure I followed the steps from their documentation.
Thank you in advance for helping! This is my first time asking help from StackOverflow :)
I have completed the steps provided by Google to implement push notifications using the Drive API. Everything goes as described except I do not receive any notifications from Google whenever a change occurs (listed as Step 4 below).
Below is a summary of the steps I followed and the results at each stage:
Step 1 (Success): Verify that you own the domain
I went to Search Console as directed and successfully verified the site.
Step 2 (Success): Register your domain
I visited the Domain Verification page and added my domain in the API Console. Google API Console updated to show the list with my desired domain.
I have a valid SSL certificate from GoDaddy for my domain and have verified that as well.
Step 3 (Success): Created my notification channel
Using the python SDK, I created the channel and got back a response from Google. The python code below shows how I did this. Note: The address included in my request directs Google to send notifications to my Flask website that I am hosting in Google's App Engine. I am showing the domain as my_domain.com here for privacy as I am in the process of bringing up the site.
channel_id = str(uuid.uuid4())
body = {
'kind': 'api#channel',
'id': channel_id,
'type': 'web_hook',
'address': "https://my_domain.com/notifications"
}
try:
start_page_token = service.changes().getStartPageToken().execute().get('startPageToken');
print(service.changes().watch(pageToken=start_page_token, body=body).execute())
except Exception as e:
print('Exception: {}'.format(e))
And here is the response from Google:
{u'resourceId': u'vTMwJWl7a23Af1LK1TpJZ38eO_0', u'kind': u'api#channel', u'expiration': u'1538433975000', u'id': u'dce14225-95c8-42a7-9945-4fed1604c848', u'resourceUri': u'https://www.googleapis.com/drive/v3/changes?alt=json&includeCorpusRemovals=false&includeRemoved=true&includeTeamDriveItems=false&pageSize=100&pageToken=4919&restrictToMyDrive=false&spaces=drive&supportsTeamDrives=false&alt=json'}
Step 4 (Failed): Process a notification from Google
I am running a Flask website on App Engine and created an app.route to receive notifications from Google. I've included the code below. I never receive anything at this URL.
#app.route('/notifications')
def notifications():
print('in notifications()')
try:
chan_id = request.args.get('X-Goog-Channel-ID', 'empty')
msg_num = request.args.get('X-Goog-Message-Number', 'empty')
rid = request.args.get('X-Goog-Resource-ID', 'empty')
state = request.args.get('X-Goog-Resource-State', 'empty')
resource_uri = request.args.get('X-Goog-Resource-URI', 'empty')
goog_changed = request.args.get('X-Goog-Changed', 'empty')
goog_chan_exp = request.args.get('X-Goog-Channel-Expiration', 'empty')
goog_chan_token = request.args.get('X-Goog-Channel-Token', 'empty')
print('chan_id: {}'.format(chan_id))
print('msg_num: {}'.format(msg_num))
print('rid: {}'.format(rid))
print('state: {}'.format(state))
print('resource_uri: {}'.format(resource_uri))
print('goog_changed: {}'.format(goog_changed))
print('goog_chan_exp: {}'.format(goog_chan_exp))
print('goog_chan_token: {}'.format(goog_chan_token))
except Exception as e:
print('notifications() exception: {}'.format(e))
print('leaving notifications()')
return jsonify(result='done')
I'm not sure if this is the problem but, when I tried Google Drive push notifications, I didn't use the SDK. I only made a POST request as it shows in the documentation.
Could you try to make a POST request to "https://www.googleapis.com/drive/v3/changes/watch" instead of using service.changes().watch()?
You will need an access_token for the request, but I believe you wont need to send a startPageToken as they don't use it in their example:
I hope it helps!
So, I'm a bit confused on how I get past authentication on Youtube using Python and successfully login. I always get error 403 when I try to PragmaticLogin():
yt_service = gdata.youtube.service.YouTubeService()
service.developer_key = 'MY Key'
service.client_id='My ID'
service.email = 'myemail#yahoo.gr'
service.password = 'mypassword'
service.source = 'my_program'
service.ProgrammaticLogin()
What do I have to do?
Update:
I think that it has to do with authentication. Do I need both developer_key and client_id? Where do I get each? I want to have rights to add comments to my videos etc.
Full error:
Traceback (most recent call last):
File "/home/bodhi32/Documents/bot.py", line 9, in <module>
client.ClientLogin(USERNAME, PASSWORD)
File "/usr/lib/pymodules/python2.7/gdata/service.py", line 833, in ClientLogin
self.ProgrammaticLogin(captcha_token, captcha_response)
File "/usr/lib/pymodules/python2.7/gdata/service.py", line 796, in ProgrammaticLogin
raise Error, 'Server responded with a 403 code'
gdata.service.Error: Server responded with a 403 code
ClientLogin is deprecated and has all sorts of errors. Don't use it.
Use OAuth2.
This sample should get you started:
https://github.com/youtube/api-samples/blob/master/python/my_uploads.py
Use your code but make sure you fill the developer_key and client_id fields ( check below how to get them).
yt_service = gdata.youtube.service.YouTubeService()
service.developer_key = 'MY Key'
service.client_id='My ID'
service.email = 'myemail#yahoo.gr'
service.password = 'mypassword'
service.source = 'my_program'
service.ProgrammaticLogin()
To obtain a youtube api go to
https://cloud.google.com/console/project and create a new project, then enable youtube.
Check this video for more info Obtaining a simple API key for use with the YouTube API
BELIEVED SOLVED: Python API only supports v1, while watch later was added in v2. SOURCE
SOLUTION: Use "Experimental" API v3
I am attempting to use the Youtube API to access my Watch Later playlist. Below is the code I am using.
import gdata.youtube
import gdata.youtube.service
yt_service = gdata.youtube.service.YouTubeService()
yt_service.ssl = True
yt_service.developer_key = 'REDACTED'
yt_service.email = 'REDACTED'
yt_service.password = 'REDACTED'
yt_service.ProgrammaticLogin()
playlist_uri = 'https://gdata.youtube.com/feeds/api/users/default/watch_later?v=2'
playlist_video_feed = yt_service.GetYouTubePlaylistVideoFeed(uri=playlist_uri)
for playlist_video_entry in playlist_video_feed.entry:
print playlist_video_entry.title.text
I am receiving the following error.
Traceback (most recent call last):
File "Youtube.py", line 21, in <module>
playlist_video_feed = yt_service.GetYouTubePlaylistVideoFeed(uri=playlist_uri)
File "/Library/Python/2.6/site-packages/gdata/youtube/service.py", line 393, in GetYouTubePlaylistVideoFeed
uri, converter=gdata.youtube.YouTubePlaylistVideoFeedFromString)
File "/Library/Python/2.6/site-packages/gdata/service.py", line 1108, in Get
'reason': server_response.reason, 'body': result_body}
gdata.service.RequestError: {'status': 400, 'body': 'Invalid request URI', 'reason': 'Bad Request'}
It would seem the URI https://gdata.youtube.com/feeds/api/users/default/watch_later?v=2 is invalid. However this is the one stated to be used in the google documents. Am I using it wrong, or is there another issue here?
In addition if I change the URI to http://gdata.youtube.com/feeds/api/playlists/63F0C78739B09958 it works as expected.
You should check your authentication. According to Retrieving and updating a user's 'Watch Later' playlist:
Again, the link will only be present in a profile entry if either of
the following conditions is true:
You submit an authenticated request to retrieve the logged-in user's
own profile.
The watch_later playlist is publicly available for the user whose
profile you are retrieving.
The API server will return a 40x HTTP response code if you try to
retrieve a watch_later playlist and neither of the above conditions is
true.
The second link would work most likely due to the second publicly available condition being met. One thing I do notice missing from your example is the client id/source:
# A complete client login request
yt_service.email = 'jo#gmail.com'
yt_service.password = 'mypassword'
yt_service.source = 'my-example-application'
yt_service.developer_key = 'ABC123...'
yt_service.client_id = 'my-example-application'
yt_service.ProgrammaticLogin()
You should look into that and ensure that your authentication is happening properly.