I was trying to figure out how to use the PyGithub module, but I keep getting the same error:
github.GithubException.GithubException: 401 {"message": "Requires authentication", "documentation_url": "https://docs.github.com/rest/reference/users#get-the-authenticated-user"}
My code is pretty simple, considering I just started out:
from github import Github
g = Github("Charonum","xxxxxxxx")
user = g.get_user()
print(user.name)
print(user.login)
The error is when it gets to print(user.name).
Looking at their documentation, it doesn't look like you're initializing the Github class correctly. I would read through that to find more about how to properly setup. The error is pretty clear that you don't have your authentication credentials input properly.
Example from the documentation:
from github import Github
# using an access token
g = Github("access_token")
# Github Enterprise with custom hostname
g = Github(base_url="https://{hostname}/api/v3", login_or_token="access_token")
Related
Getting the issue below, I can get the repositories but when I try get the commit's, that is when it fails and I get a 401. Repo's and workspace are private so the first one would fail I imagine if there was an issue with the code entirely. Anyone done this recently?
baseUrlv2 = "https://bitbucket.org/api/2.0"
role = "contributor"
username = {bitbucket_username}
password = {bitbucket_app_password}
r = requests.get("{base}/repositories/{workspace}?role={role}".format(base=baseUrlv2, role=role),
auth=(use
rname, password))
repos = r.json()
while 'next' in repos:
for repo in repos["values"]:
commitLink = repo["links"]["commits"]["href"]
repoSlug = repo["slug"]
#the request below seems to be the issue :\
r = requests.post((commitLink), auth=(username,password))
c = r.json() #this is where it breaks
After a bunch of trial and error wherein the Atlassian documentation says use your Bitbucket username + App Password.. This wasn't the case.
You need to use your normal email and password to actually use the API.
Some strange issues in general with the API calls for Bitbucket but this issue is resolved.
I attempted to run the code below and am getting an error that states:
HTTP Error code: 403: Forbidden: Authentication succeeded but account is not authorized to access this resource.
from searchtweets import ResultStream, gen_rule_payload, load_credentials, collect_results
import requests
premium_search_args = load_credentials("/home/dirname/twitter_keys.yaml",
yaml_key="search_tweets_premium",
env_overwrite=False)
rule = gen_rule_payload("basketball", results_per_call=100) # testing with a sandbox account
print(rule)
from searchtweets import collect_results
tweets = collect_results(rule,
max_results=100,
result_stream_args=premium_search_args)
# print(tweets.all_text)
[print(tweet.all_text, end='\n\n') for tweet in tweets[0:10]];
My YAML file looks like this:
search_tweets_premium:
account_type: premium
endpoint: https://api.twitter.com/1.1/tweets/search/fullarchive/dev.json
consumer_key: AAAAAAAAAAAAAAAAAAAAA
consumer_secret: BBBBBBBBBBBBBBBBBBBBBBBBBBB
Only other thing to note is that I am using the free/sandbox service.
Any ideas if I am doing anything wrong in the code, the YAML, and/or within my Twitter developer account?
You'll need to go to https://developer.twitter.com/en/account/environments
There you should be able to see the various development environments that you have. You can create one should they not have been created.
The dev environment label would then be the thing you use to replace in your endpoint.
In my example, it would be:
https://api.twitter.com/1.1/tweets/search/fullarchive/development.json
If that still doesn't work, you might need to include a bearer token in your YAML file.
I have a mendeley account which I am using from their online version. I created a userid and client secret, saved it in config.yml file from and using it to authenticate. I am using the below code available on their website
import yaml
from mendeley import Mendeley
with open('config.yml') as f:
config = yaml.load(f)
REDIRECT_URI = 'http://localhost:5000/oauth'
mendeley = Mendeley(config['clientId'], config['clientSecret'], REDIRECT_URI)
auth = mendeley.start_client_credentials_flow()
session = auth.authenticate()
This code works fine and I got not errors. But when I am trying to access data using the commands in the example it throws error. For example
>> print (session.profiles.me.display_name)
mendeley.exception.MendeleyApiException: The Mendeley API returned an error (status: 400, message: No userid found in auth token)
>> for document in session.documents.iter():
print document.title
mendeley.exception.MendeleyApiException: The Mendeley API returned an error (status: 403, message: Access to this document is not allowed)
I am stuck here and do not know how to access the data or articles I have on mendeley fom its API. Any help will be highly appreciated.
You are using the Client Credentials flow (a term from OAuth). Quoting from the docs:
This flow does not require the user to log in. However, it only
provides access to a limited set of resources (the read-only Mendeley
Catalog of crowd sourced documents).
The two calls from your questions are private (user) information, and will therefor fail.
On the other hand, the following should work:
print session.catalog.by_identifier(doi='10.1371/journal.pmed.0020124', view='stats').reader_count
If you want to access private information, you will need users to log in. This question should help.
I've tried several things from the documentation and it hasn't worked. This code is from the documentation of https://github.com/facebookarchive/python-instagram/blob/master/README.md
api = InstagramAPI(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
popular_media = api.media_popular(count=20)
for media in popular_media:
print media.images['standard_resolution'].url
Here is the error:
in _do_api_request
raise InstagramAPIError(status_code, content_obj['meta']['error_type'],
content_obj['meta']['error_message'])
instagram.bind.InstagramAPIError: (400) OAuthAccessTokenException-The
access_token provided is invalid.
Take a look at the README file on that GitHub repo. Here it explains what you need to do to obtain an Access Token.
I'm trying to use Facepy, a sort of API for Facebook and Python, to simply post an image from my desktop to a Facebook page for which I have the authorization code and publish_stream and upload_photo permissions. (UPDATE: how do I verify that that is true?)
It's not working for me, though it is working for the author of Facepy. I'm at a loss for what is causing the issue. When I run this code, taken from the Facepy site (and using a .jpg on my computer):
UPDATE: This is the entirety of the code I am running:
from facepy import GraphAPI
print 'Trying Facebook page...'
my_token = 'xxxxxxxxxxxxxxxxx'
graph = GraphAPI(my_token)
# Get my latest posts
my_posts = graph.get("me/posts")
#Post a photo of a parrot
graph.post(path = "me/photos",source = open("python.png"))
print 'Done.'
Facepy returns this error:
Error: (#1) An unknown error occurred
I have tried it, unsuccessfully, with Python 2.5 and Python 2.7 on WinXP. Facepy can, however, get my latest posts, with graph.get('me/posts')
Any advice to get this to work would be appreciated.
I had the same problem. And apparently found the answer.
According to GitHub page, a source parameter needs a rb mode:
graph.post(
path = 'me/photos',
source = open('parrot.jpg', 'rb')
Works for me (with Python 3 & Graph API 2.8).
To publish from URL, you can use url:
par = {
"caption": "Some text",
"url": "https://example.com/1.jpg"}
send_post = graph.post(path='me/photos', **par)