spring-boot我可以使用FacebookAuthenticationService连接apiKey和appSecret吗?(spring-boot can I use FacebookAuthenticationService to connect with apiKey and appSecret?)
在基于spring-boot的应用程序中,我会让facebook用户朋友使用apiKey和appSecret来连接和facebook用户ID来识别用户(没有用户访问令牌)。
我知道通过facebook api graph我可以使用facebook api endpoint / {user-id} / friends获取用户好友列表,我可以使用app令牌代替用户访问令牌。
我试着像这样实现Facebook:
import org.springframework.social.facebook.api.Facebook; import org.springframework.social.facebook.security.FacebookAuthenticationService; ... Facebook facebook = new FacebookAuthenticationService(apiKey, appSecret);
但是当我在我的代码中包含这一行时,我得到了这个编译错误:
无法解析org.springframework.social.security.provider.OAuth2AuthenticationService类型。 它是从所需的.class文件间接引用的
在FacebookAuthenticationService.class里面有导入
import org.springframework.social.security.provider.OAuth2AuthenticationService;
在构建路径中不存在....我找不到
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-social-security</artifactId>
喜欢不是弹簧靴
<groupId>org.springframework.social</groupId> <artifactId>spring-social-security</artifactId>
但我不能包括第二个'因为我会在实例化时得到一个不同的错误
Facebook facebook = new FacebookAuthenticationService(apiKey, appSecret);
错误是:
类型不匹配:无法从FacebookAuthenticationService转换为Facebook
我想知道是否有人可以帮助我....
In a spring-boot based application, I would get facebook user friends using apiKey and appSecret to connect and facebook user Id to identify user (no user access token).
I know that via facebook api graph I can use facebook api endpoint /{user-id}/friends to get user friends list, and I can user the app token instead of user access token.
I'm tryng to instanciate Facebook like this:
import org.springframework.social.facebook.api.Facebook; import org.springframework.social.facebook.security.FacebookAuthenticationService; ... Facebook facebook = new FacebookAuthenticationService(apiKey, appSecret);
but when I include this line in my code I got this compile error:
The type org.springframework.social.security.provider.OAuth2AuthenticationService cannot be resolved. It is indirectly referenced from required .class files
inside FacebookAuthenticationService.class there is the import of
import org.springframework.social.security.provider.OAuth2AuthenticationService;
that doesn't exists in build path.... I can't find a
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-social-security</artifactId>
like the not spring-boot
<groupId>org.springframework.social</groupId> <artifactId>spring-social-security</artifactId>
but I can't include the second one 'cause I will get a different error durind instantiation of
Facebook facebook = new FacebookAuthenticationService(apiKey, appSecret);
the error is:
Type mismatch: cannot convert from FacebookAuthenticationService to Facebook
I'm wondering if someone can help me....
原文:https://stackoverflow.com/questions/35612334
最满意答案
这些行几乎是错误的(因为
call_addone
是用inlineCallbacks
定义的):d = call_addone(4) d.callback(0) # whatever I pass is set as deferred's result
随着这些行变得很奇怪(因为不使用
inlineCallbacks
):d = addone(2) d.callback(0) # whatever I pass is ignored
在这两种情况下,您都在要求一些库代码创建一个
Deferred
,然后您的应用程序代码将为Deferred
提供一个结果。 由于addone
做你想做的事情,很明显可以使用Deferred
这种方式。 但是,这不是一个好习惯。 最好的做法是负责创建和解雇一个Deferred
在同一个地方。 所以,在这种情况下,随着addone
的实现。你失败的测试失败的原因是你没有考虑到
inlineCallbacks
的实现,它认为它确实负责inlineCallbacks
Deferred
返回。 而Deferred
它返回的不是由它所做的addone
调用返回的Deferred
。 它保持了自我Deferred
。
d
的结果在:d = call_addone(4) d.callback(0) # whatever I pass is set as deferred's result
为
0
原因正是因为您提供了0的结果。您已将结果提供给inlineCallbacks
管理的Deferred
并短路了inlineCallbacks
所有其他实施。 如果由addone
返回的Deferred
确实触发过,那么您可能会看到AlreadyCalledError
因为inlineCallbacks
会尝试将结果提供给d
,而Deferred
已经提供了结果。These lines are pretty much wrong (given that
call_addone
is defined withinlineCallbacks
):d = call_addone(4) d.callback(0) # whatever I pass is set as deferred's result
This goes along with these lines being weird (given that
inlineCallbacks
is not used):d = addone(2) d.callback(0) # whatever I pass is ignored
In both cases, you are asking some library code to create a
Deferred
and then your application code is supplying a result for theDeferred
. Sinceaddone
does what you want, clearly it's possible to useDeferred
this way. However, it's not a good practice. Best practice is for the responsibility for creating and firing aDeferred
to lie in the same place. So, in this case, with the implementation ofaddone
.The reason your failing test fails is that you've not accounted for the implementation of
inlineCallbacks
which believes that it is indeed responsible for firing theDeferred
it returns. And theDeferred
it returns is not theDeferred
returned by theaddone
call it makes. It keeps thatDeferred
to itself.The result of
d
in:d = call_addone(4) d.callback(0) # whatever I pass is set as deferred's result
is
0
precisely because you've supplied a result of 0. You've supplied the result to theinlineCallbacks
-managedDeferred
and short-circuited all of the rest of the implementation ofinlineCallbacks
. If theDeferred
returned byaddone
did ever fire, you would likely see anAlreadyCalledError
becauseinlineCallbacks
would try to supply a result tod
, theDeferred
you already supplied a result to.
相关问答
更多-
如何从扭曲的inlineCallbacks修饰函数中捕获异常?(How to catch exceptions from a twisted inlineCallbacks decorated function?)[2024-01-17]
在重新阅读有关Twisted Deferred回调和errbacks的文档后想出来。 上面代码和链接gist中的问题是回调和错误回复的缺失参数。 如果我用以下代码替换上面写的内容,则异常被捕获并通过errback按预期通知: def cb(result): pass def eb(failure): pass Figured it out after rereading the docs about Twisted Deferred callbacks and errbacks. The ... -
SSL的扭曲试验错误(Twisted trial error with SSL)[2022-06-01]
Jonathan Lange写到了这个问题及其解决方案 。 您可能还需要考虑在单元测试中不使用真实的网络连接。 Agent已经有效了。 Site , reactor.listenSSL等也是如此。尝试编写运行代码的单元测试,而不是代码所依赖的库中的大量代码。 Jonathan Lange wrote about this problem and its solutions. You may also want to consider not using real network connections in ... -
你嘲笑time的方式确实有问题。 mock装饰器在输入测试函数时设置补丁,然后在退出时删除它们。 但是,您将返回Deferred ,这意味着time.time回调在测试函数退出后运行良好。 如果你只想继续使用类似于模拟的补丁界面,可以使用Twisted的TestCase.patch ,我相信这会考虑Deferreds 。 然而, 模拟是一种测试反模式 ,处理时间流逝的更好方法是使用记录的API来测试时间的流逝 , twisted.internet.task.Clock 。 There is indeed s ...
-
擦洗单元测试(Scrapy Unit Testing)[2023-09-09]
我做的方式是创建假的响应,这样你可以离线测试解析函数。 但是您可以通过使用真正的HTML来获得真实的情况。 这种方法的一个问题是您的本地HTML文件可能无法反映在线的最新状态。 所以如果HTML在线改变你可能有一个很大的错误,但你的测试用例仍然会通过。 所以这可能不是最好的方式来测试这种方式。 我目前的工作流程是,每当发生错误时,我都会发送一封电子邮件给admin,并附上该URL。 那么对于那个具体的错误我创建一个html文件的内容导致错误。 然后我创建一个单元测试。 这是我用于从本地html文件创建样本S ... -
这些行几乎是错误的(因为call_addone是用inlineCallbacks定义的): d = call_addone(4) d.callback(0) # whatever I pass is set as deferred's result 随着这些行变得很奇怪(因为不使用inlineCallbacks ): d = addone(2) d.callback(0) # whatever I pass is ignored 在这两种情况下,您都在要求一些库代 ...
-
您可以通过编写报告插件任意格式化试验输出。 你已经找到了该插件的界面 - IReporter 。 一旦你编写了这样一个插件,你就可以通过在trial命令行参数中添加--reporter=yourplugin来使用它。 您可以使用trial --help-reporters查看系统上已有的报告插件列表。 如果您安装了python-subunit那么您将看到subunit ,它是一种机器可解析的格式,可能已满足您的要求。 不幸的是,它仍然是一个亚单位v1记者,亚单位v2在很多方面都更好。 不过,它可能就足够了。 ...
-
首先:你不能打电话给你的顶级单元测试包test 。 这是Python的单元测试的名称,因此您永远无法在已安装的配置中运行测试,并且根据您的python的设置方式,您可能最终导入python自己的测试而不是您自己的测试。 第二: sys.path是一个巨大而微妙的谜团。 trial支持在文件和目录上运行,作为快速入门的黑客攻击,但使用路径名永远不会完全正确。 正确的做法是传递一个模块 (或包)名称,它可以作为python模块导入并检查。 因此,如果您的目录结构如下所示: ~/Projects/MyProjec ...
-
Deferred类有大量的try / except魔法,以至于错误很好地从最终用户中提取出来,并且只能通过addErrback轻松获得。 如果发生错误,则表明您已经破坏了功能。 在同步应用程序中,您可以将“问题部分”封装在try / except块中。 在inlineCallbacks的情况下,可以使用相同的技术: try: run_test() except Exception as err: # handle error here finally: reactor.stop() ...
-
用inlineCallbacks修饰的生成器函数返回Deferred - 而不是生成器。 情况总是如此。 您永远不能从使用inlineCallbacks修饰的函数返回生成器。 请参阅twisted.spread.util的寻呼机类,了解您可以采取的另一种方法。 A generator function decorated with inlineCallbacks returns a Deferred - not a generator. This is always the case. You can ne ...
-
当然可以使用真正的Agent 。 您必须使用reactor作为第一个参数构造Agent实例; 因此,您可以提供假反应堆,例如MemoryReactor 。 虽然这是一个非常方便的方式来了解Agent正在做的事情,但它对于测试Agent本身来说确实非常有用。 注入一个假Agent很多次是一个更容易的路径,因为它的api都很浅(几乎所有东西都通过Agent.request和简单(它返回一个IResponse )) it is certainly possible to use a real Agent. You ...