I'm trying to automatically setup git system and I'm stuck in a process where I want to add user's key using the github api. This is what I've so far.
USER_SSH_PUB=glob.glob(os.path.expanduser('~/.ssh/temp.k.pub'))
user_Ssh_Pub_Key_File=open(USER_SSH_PUB[0],"r")
GITHUB_URL='https://api.github.com/users/abc/keys'
key_Data=urllib.urlencode({"title":"abcd","key":user_Ssh_Pub_Key_File.read()})
request=urllib2.Request(GITHUB_URL,key_Data) response=urllib2.urlopen(request) |
print response.read()
I get a 404 when I do this. Has anybody done this ?
I assume you want to take a public key and add that to a User's set of keys, i.e., through this API.
The problem is that you can only do this for the authenticated user, you can not do this on the behalf of a different user. So your GITHUB_URL would have to be https://api.github.com/user/keys and you would have to authenticate as user abcd in order to do that.
I don't think there are any python wrappers for the API using urllib2 which work (well), but there are a few listed here which includes mine which is pip-installable. With my library, your code would look like:
from github3 import login
g = login('abcd', password)
with open('~/.ssh/temp.k.pub', 'r') as fd:
key = g.create_key('abcd', fd)
print("Created {0}".format(key.title))
There are other popular wrappers like pygithub3 but I'm not familiar with them.
Related
I am attempting to retrieve and add function/host keys for an Azure Government function app via Python. I am currently working with the information from this question and the corresponding API page. While these are not specific to Azure Government, I would think the process would be similar after updating the URLs to the Azure Government versions. However, I am receiving the error "No route registered for '/api/functions/admin/token'" when running the jwt part of the given code. Is this approach feasible for what I am trying to do?
I also found somewhere that I instead might want to try a GET request like this:
resp = requests.get("https://management.usgovcloudapi.net/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.Web/sites/<function-app-name>/functions/admin/masterkey?api-version=20XX-XX-XX", headers={"Authorization": f"Bearer {something}"})
This gives me the error "{"error":{"code":"InvalidAuthenticationToken","message":"The access token is invalid."}}", though. If this is indeed the correct approach, then what format should the Bearer token take?
Bit late answering but it may be useful for someone else in the future, it took me a while to find out how to do this.
If you want to retrieve the keys of a specific function within a function app then you can use list_function_keys() function from the Python SDK
Working with the Az management API directly may be a bit annoying and since the Azure CLI is written in Python whatever operation you do with the CLI you can do it directly in a Python script.
Here's an example of how you can retrieve the keys
from azure.identity import DefaultAzureCredential
from azure.mgmt.web import WebSiteManagementClient
# Your subscription ID
SUB_ID = "00000000-0000-0000-0000-000000000000"
fn_name = "some_function" # Name of your function
app_name = "some_app" # Name of your site/function app
rg_name = "some_rg" # Resource group name to which the function belongs
web_client = WebSiteManagementClient(subscription_id=SUB_ID, credential=DefaultAzureCredential())
keys = web_client.web_apps.list_function_keys(rg_name, app_name, fn_name)
# Your keys will be accessible in the additional_properties param
print(keys.additional_properties)
Hope it helps! I'm new on Azure so if I'm doing something wrong, please don't hesitate to point out my mistake and share your correction
I have been attempting to use PyGitHub to edit/update my repository files using PyGitHub, however (I guess I tried to access it too many times :P) it gives me this error:
github.GithubException.RateLimitExceededException: 403 {"message": "API rate limit exceeded for xx.xx.xx.xxxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", "documentation_url": "https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}
I was accessing the data just fine before. I had researched it and in the effort to fix the problem I had replaced my username with an access token. Here is my full code:
from github import Github
# Github username
username = "JosephCWatkins"
password = yeoldepasswordhere
# pygithub object
g = Github(yeolaccesstokenhere, password)
user = g.get_user(username)
repof = g.get_repo("JosephCWatkins/moonspellprojectprogress")
#me trying to access the data :P:
contents = repof.get_contents("test.txt")
print(contents.content.split.__doc__())
It says that I must authenticate myself which would give me a higher rate limit, but the best I could figure out on trying to solve it was adding the access token. Am I doing something wrong? How can I authenticate myself so I can continue working with the API? And also, if you could help me be able to print the contents of a repository file. Thank You. :)
The first part of my question is still unanswered, but I accessed the contents of my GitHub Repository File with this code:
repo = g.get_repo("username/repositoryname")
contents = repo.get_contents("filename.filetype").decoded_content
I know it sounds simple but the documentation does not go into the various functions like it should, it mostly only gives examples. Plus the internet lacks a simple answer to this problem. For anyone who wants to access their GitHub file via PyGitHub, here it is.
I am unable to retrieve the documents which are available in my collection inside the firestore database. Here is my code.
Every time I run this console dosen't print anything. I am following the documentation avaliable on this link https://firebase.google.com/docs/firestore/query-data/get-data, but it dosen't seems to work.
database_2 = firestore.client()
all_users_ref_2 = database_2.collection(u'user').stream()
for users in all_users_ref_2:
print(u'{} => {}'.format(users.id, users.to_dict()))
Do you have multiple projects? If so, double check that you open a client to the correct project. One quick way to confirm is to pass the project ID to the client:
db = firestore.Client('my-project-id')
Could be an authentication issue, you could download a service account key and use that in your project at the top.
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALs"] = "path/to/key.json"
or as mentioned
database_2 = firestore.Client("<project ID>")
make sure Client is a capital C
I want information from groups where I am and I can not get it. Always the code returns an empty data.
{u'data': []}
My code is as follows:
import facebook
graph = facebook.GraphAPI(access_token='my token')
a = graph.get_object('me/groups')
print(a)
What is wrong? I'm using Python 2.7.10 and Facebook official sdk.
I've tried with facepy and it's the same, empty data.
Is it a bug?
First you question isn't complete. You need to show what permissions you are asking for and what API version you are using. That said I suspect one of two things.
You are not using the necessary permission "user_managed_groups".
Or
You don't realize that with that permission that "groups" edge will only return the groups which the user is an admin of.
It is not possible to do anything with a group that the logged in user is not an admin of via the API. Learn about me/groups HERE
Im trying to send a simple email to do the password recover of a user, the input is just a email to send the new password..
But i can't... i get this error
SMTPServerDisconnected: please run connect() first
I already tried a few examples, like, https://bitbucket.org/andialbrecht/appengine_emailbackends/overview, but i get the same error
I really need this, maybe someone can tell me how to use an alternative to code in my view to send an email...Also i changed the backend to
EMAIL_BACKEND = 'djangoappengine.mail.EmailBackend'
but nothing,i don't know how to use this backend anyway :(
Plz Help :(
maybe someone can tell me how to use an alternative to code in my view to send an email...
I can help with this, seeing as it seems that perhaps this repository you're trying to use is based on an earlier version of App Engine and is throwing the error due to a required code change somewhere in the library - either that or the fact that you changed the string from what the library recommends (your version: 'djangoappengine.mail.EmailBackend') to a string that seems to not be correct, as it's different to what the author of the repository directed you to use (their version: 'appengine_emailbackend.EmailBackend'), and this is causing trouble.
Whenever possible, I'd recommend seeing if there is an "app-engine-y" way to do something, before going to a third-party library or deploying a module somebody else wrote to hack in third-party capabilities, or looking for an advanced/experimental feature (for example, use Datastore first, rather than remotely connecting to a MySQL VM, unless you need MySQL). If you absolutely need that library, this is a different story, but if you just want to send emails, the Mail API is what you need. It's a convenient way to send emails on App Engine.
I'm going to assume in the following that you are storing your user's usernames and hashed passwords in custom-defined User-kind entities in your Datastore. If you have your users using simple OAuth to sign into your site, there is never any reason to "reset/recover password":
Create the <form action="/some/route" action="POST"> element on
the page where the user requests password recovery.
Put the code responsible for handling this form submission (they will input their email, or whatever account info they need for your code to find their User entity in the Datastore in a handler that will respond on that route.
In the handler, generate a unique token and store it in the Datastore. Send the token in the email that you generate and send using the Mail API (see the example code in the link to the docs I provided). This will allow your user to return to your site, authenticate with the token from the email, and then fill out a form to create a new password. You will then hash this password (with a salt) and store it in their User entity in your Datastore.
I'm skipping over the details of how to implement a "password recovery form", given what I said about OAuth and that you are probably really only concerned with how to send mail. In the email you send, for example, you can insert a hyperlink to your site with the token already inserted as a query param, so that the user doesn't have to copy and paste, etc.