Facebook API for Java Spring Framework (2013-10-08)

Programming 2012.10.08 21:06

페이스북 API들이 많은데.. 그중에 자바가 제대로 된게 없어서 고생을 하곤했는데..

최근에 다시 검색해보니..

으아니~! Springframework에서도 페이스북 뿐만 아니라 각종 Social용 API를 포함하는 라이브러리를 선보였넹!!


http://www.springsource.org/spring-social


일단 위 페이지가 공식 페이지!


여기서 나는 페이스북 API가 필요하다능!


자~ 그럼 한번 같이 만들어봅시다.


따라하기 전에 먼저...Facebook Developer 사이트에 가셔서 자신의 App을 만들어야 합니다! 이건 구글에 검색해보면 무지 많으니까 찾아보시오!

https://developers.facebook.com/apps


1. 필요한 라이브러리를 Maven으로 가져옵니다.

<properties>

...

<spring-social.version>1.0.2.RELEASE</spring-social.version>

<org.springframework.social-facebook-version>1.0.2.RELEASE</org.springframework.social-facebook-version>

</properties>


...


<dependency>

<groupId>org.springframework.social</groupId>

<artifactId>spring-social-core</artifactId>

<version>${spring-social.version}</version>

</dependency>

<dependency>

<groupId>org.springframework.social</groupId>

<artifactId>spring-social-web</artifactId>

<version>${spring-social.version}</version>

</dependency>

<dependency>

<groupId>org.springframework.social</groupId>

<artifactId>spring-social-facebook</artifactId>

<version>${org.springframework.social-facebook-version}</version>

</dependency>



2. 코드를 작성합니다.

( 공식 document에서는 - http://static.springsource.org/spring-social-facebook/docs/1.0.x/reference/html/connecting.html  Spring의 환경에 맞게 xml로 설정을 불러오게 되어있는데 저는 일단 만들어 테스트해보는게 목적이니까! JunitTest로 그냥 고고싱!)

import java.util.Calendar;

import java.util.List;


import org.junit.Before;

import org.junit.Test;

import org.springframework.social.connect.support.ConnectionFactoryRegistry;

import org.springframework.social.facebook.api.Facebook;

import org.springframework.social.facebook.api.FacebookProfile;

import org.springframework.social.facebook.api.impl.FacebookTemplate;

import org.springframework.social.facebook.connect.FacebookConnectionFactory;


public class FacebookAPITest {


    @Before

    public void setUp() throws Exception {

    }


    @Test

    public void test() {

Long st = Calendar.getInstance().getTimeInMillis();

ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();

registry.addConnectionFactory(new FacebookConnectionFactory("앱 ID/API 키", "앱 시크릿 코드"));

String accessToken = "액서스토큰"; // access token received from Facebook after OAuth authorization

Facebook facebook = new FacebookTemplate(accessToken);

FacebookProfile profile = facebook.userOperations().getUserProfile();

String myid = profile.getId();

String myname = profile.getName();

System.out.println("my id is '"+myid+"' and my name is '"+myname+".");

System.out.println("============= timestamp : "+(Calendar.getInstance().getTimeInMillis() - st));

st = Calendar.getInstance().getTimeInMillis();

List<FacebookProfile> friends = facebook.friendOperations().getFriendProfiles();

for(FacebookProfile fp : friends){

    System.out.println("Friend name => "+fp.getName());

}

System.out.println("============= timestamp : "+(Calendar.getInstance().getTimeInMillis() - st));

st = Calendar.getInstance().getTimeInMillis();

List<String> fids = facebook.friendOperations().getFriendIds();

for(String id : fids){

    System.out.println("friend id => "+id);

}

System.out.println("============= timestamp : "+(Calendar.getInstance().getTimeInMillis() - st));

st = Calendar.getInstance().getTimeInMillis();

    }


}


3. 실행~


my id is '1178*****' and my name is '*****'.

============= timestamp : 2156

Friend name => J***

Friend name => J****

Friend name => 김****

Friend name => Jon****

Friend name => Ch****

Friend name => Ha****

...

...

...



아...정말.정말.정말.정말 쉬워졌다....-_-!!

저작자 표시 비영리 변경 금지
신고
크리에이티브 커먼즈 라이선스
Creative Commons License

[flex]Destination 못찾는 에러

Programming/Flex 2008.11.28 18:35

에러 내용.

  1. Please Contact with message. :)
    faultCode:InvokeFailed faultString:'[MessagingError message='Destination 'memberDao' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']' faultDetail:'Couldn't establish a connection to 'memberDao''

 

그림_1.png

 

위와 같은 에러가 났다.

 

원인

컴파일러가 services-config.xml을 못찾아서 나는에러다.

 

해결책

Navigator로 프로젝트를 보면 루트에 .actionScriptProperties라는 파일이 보인다.

이녀석을 오픈해서 열어보면 대충 아래와 같은 내용돌이 있는데

<?xml version="1.0" encoding="UTF-8"?>
<actionScriptProperties mainApplicationPath="emt.mxml" version="3">
<compiler additionalCompilerArguments="-locale en_US" copyDependentFiles="true" enableModuleDebug="true" generateAccessible="true" htmlExpressInstall="true" htmlGenerate="true" htmlHistoryManagement="true" htmlPlayerVersion="9.0.124" htmlPlayerVersionCheck="true" outputFolderPath="bin-debug" rootURL="http://localhost:8080/" sourceFolderPath="flex_src" strict="true" useApolloConfig="false" verifyDigests="true" warn="true">
...
 ...
</applications>
<modules/>
<buildCSSFiles/>
</actionScriptProperties>
 

빨간 줄 친 저부분을 수정해 줘야 한다.

<compiler additionalCompilerArguments="-services &quot;/프로젝트 절대경로/htdocs/WEB-INF/flex/services-config.xml&quot; -locale en_US" copyDependentFiles="true" enableModuleDebug="true" generateAccessible="true" htmlExpressInstall="true" htmlGenerate="true" htmlHistoryManagement="true" htmlPlayerVersion="9.0.124" htmlPlayerVersionCheck="true" outputFolderPath="bin-debug" rootURL="http://localhost:8080/" sourceFolderPath="flex_src" strict="true" useApolloConfig="false" verifyDigests="true" warn="true">


한마디로 services-config.xml의 절대경로를 넣어 줘야 한다.

 

젠장 이걸로 하루 날려 먹었다는.;;

 

 

이 글은 스프링노트에서 작성되었습니다.

신고
크리에이티브 커먼즈 라이선스
Creative Commons License

SpringFramework + Quartz

Programming 2008.04.28 17:54

quartz를 사용하기 위해 필요한 라이브러리

  1. spring.jar - http://www.springframework.org/download
  2. quartz.jar - http://www.opensymphony.com/quartz/download.action

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean">
      <property name="jobClass">
        <value>example.ExampleJob</value>
      </property>
      <property name="jobDataAsMap">
        <map>
          <entry key="timeout"><value>5</value></entry>
        </map>
      </property>
    </bean>
<bean id="exampleBusinessObject" class="example.ExampleBusinessObject"/>
<bean id="methodInvokingJobDetail"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject"><ref bean="exampleBusinessObject"/></property>
    <property name="targetMethod"><value>doIt</value></property>
    <property name="concurrent"><value>false</value></property>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <property name="jobDetail">
    <!-- see the example of method invoking job above -->  
    <ref bean="methodInvokingJobDetail"/>
  </property>
  <property name="startDelay">
    <!-- 10 seconds -->
    <value>10000</value>
  </property>
  <property name="repeatInterval">
    <!-- repeat every 50 seconds -->
    <value>50000</value>
  </property>
</bean>

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
    <ref bean="exampleJob"/>
  </property>
  <property name="cronExpression">
    <!-- run every morning at 6 AM -->
    <value>10 * * * * ?</value>
  </property>
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
    <list>
      <ref local="cronTrigger"/>
      <ref local="simpleTrigger"/>
    </list>
  </property>
</bean>   

<!--
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

    <property name="quartzProperties">
      <props>
        <prop key="org.quartz.scheduler.instanceName">RiskienhallintaScheduler</prop>
        <prop key="org.quartz.scheduler.instanceId">instance_1</prop>
        <prop key="org.quartz.scheduler.rmi.export">false</prop>
        <prop key="org.quartz.scheduler.rmi.proxy">false</prop>
       
        <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
        <prop key="org.quartz.threadPool.threadCount">3</prop>
       
        <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>

      </props>
    </property>
    <property name="triggers">
    <list>
      <ref local="cronTrigger"/>
      <ref local="simpleTrigger"/>
    </list>
  </property>
  </bean>
  -->  
</beans>


QuartzJobBean.java

package example;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class ExampleJob extends QuartzJobBean {

  private int timeout;
 
  /**
   * Setter called after the ExampleJob is instantiated
   * with the value from the JobDetailBean (5)
   */
  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }
 
  protected void executeInternal(JobExecutionContext ctx)
  throws JobExecutionException {
      System.out.println("job called. timeout is : "+timeout);
  }
}
  


ExampleBusinessObject.java

package example;

public class ExampleBusinessObject {
     
  // properties and collaborators
 
  public void doIt() {
    System.out.println("doIt method called in ExampleBusinessObject class");
   
  }
}

결과

doIt method called in ExampleBusinessObject class
job called. timeout is : 5

이 글은 스프링노트에서 작성되었습니다.

신고
크리에이티브 커먼즈 라이선스
Creative Commons License

'Programming' 카테고리의 다른 글

Quartz properties  (0) 2008.05.27
SpringFramework + Quartz  (0) 2008.04.28
wget.java  (0) 2008.03.17
simple java wget  (0) 2008.03.17


티스토리 툴바