objective-c – NSTask和Git – 权限问题

objective-c – NSTask和Git – 权限问题,第1张

概述在我的 Cocoa应用程序中,我正在尝试使用NSTask来运行一些基本的 Git命令.每当我运行一个需要权限(SSH密钥)来访问远程的命令(例如git push,git pull)时,它就会失败并出现以下错误: Permission denied (publickey). The remote end hung up unexpectedly 从终端运行相同的命令工作正常,所以我认为这可能是NST 在我的 Cocoa应用程序中,我正在尝试使用NSTask来运行一些基本的 Git命令.每当我运行一个需要权限(SSH密钥)来访问远程的命令(例如git push,git pull)时,它就会失败并出现以下错误:

Permission denIEd (publickey). The remote end hung up unexpectedly

从终端运行相同的命令工作正常,所以我认为这可能是NSTask没有设置将在访问ssh密钥的过程中使用的环境变量的问题.我尝试手动设置HOME和USER环境变量,如下所示:

[task setEnvironment:[NSDictionary dictionaryWithObjectsAndKeys:NSHomeDirectory(),@"HOME",NSUsername(),@"USER",nil]];

但这没有效果.是否有任何特定的环境变量我必须在NSTask中设置才能使其正常工作?

编辑:感谢达斯汀的提示,我进一步搞清楚了.我使用env命令列出当前会话的环境变量,我发现了这个:

SSH_AUTH_SOCK=/tmp/launch-DMQopt/Listeners

为了测试,我复制了该路径并将其设置为NSTask的环境变量并再次运行代码,这次它起作用了!也就是说,我确定每个会话的SSH_AUTH_SOCK都会发生变化,所以我不能只对它进行硬编码.如何动态生成/检索此变量?

解决方法 您可以尝试按照教程“ Wrapping rsync or SSH in an NSTask”(从 Ira开始),它提到了SSH_AUTH_SOCK变量:

Since writing this post I’ve realised that I omitted an important additional step in setting up the environment variables for the NSTask.
In order to make passwordless key-based authentication work it’s necessary to grab the SSH_AUTH_SOCK variable from the user’s environment and include this in the NSTask’s environment.
So,when setting environment variables for example;

NSTask *task;NSDictionary *environmentDict = [[nsprocessInfo processInfo] environment];// Environment variables needed for password based authentication NSMutableDictionary *env = [NSMutableDictionary dictionaryWithObjectsAndKeys:                         @"NONE",@"disPLAY",askPasspath,@"SSH_ASKPASS",username,@"AUTH_USERname",hostname,@"AUTH_HOSTname",nil];// Environment variable needed for key based authentication[env setobject:[environmentDict objectForKey:@"SSH_AUTH_SOCK"] forKey:@"SSH_AUTH_SOCK"];// Setting the task's environment[task setEnvironment:env];

但是,OP indragie评论:

I had trIEd this earlIEr but since it was being invoked with XCode,the SSH_AUTH_SOCK env var. wasn’t being passed to it.
opening the app from Finder corrects this issue.

With askPasspath being the path for the Askpass executable,which is included as part of the application’s main bundle. (In order to do this,find the executable under “Products” in xcode,and then drag it into “copy Bundle Resources” on the main application’s target.)

// Get the path of the Askpass program,which is// setup to be included as part of the main application bundleNsstring *askPasspath = [NSBundle pathForResource:@"Askpass"              ofType:@""               inDirectory:[[NSBundle mainBundle] bundlePath]];
总结

以上是内存溢出为你收集整理的objective-c – NSTask和Git – 权限问题全部内容,希望文章能够帮你解决objective-c – NSTask和Git – 权限问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1022802.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存