Before WW1, the German-Japanese relations were at an all time low. Wilhelm II feared a Japanese-united Asia and began to reduce support. Also during this period, the Russo-Japanese war further deteriorated relations because of Germany's support of Russia at the time.
With the onset of WW1, the British asked for aid from the Japanese in removing the German Navy from Chinese waters. The Japanese responded by sending Germany an ultimatum, that was unresponded. Following this, Japan formally declared war with Germany.
During WW1, Japan sieged the German-controlled port of Tsingtao which lasted for 4 months, before a combined Japanese-British force broke through and took the city. The war ended with Japan gaining much of Germany's territory in East Asia.
Before WW2, Hitler, himself, stated that Japan was a potential ally within Mein Kampf. During this time, Japan was attempting to establish their 'Greater East Asia Coprosperity Sphere'. For this plan to work, they would have to seize territory from either Russia in the north or the Western European powers in the south east. During this period, Wilhelm Solf, the German ambassador to Japan, helped to promote cultural exchanges and started to rebuild relations with Japan. Germany and Japan would later sign the Anti-Comintern Pact as a way to halt Communism and by extension, Russia. However with the onset of the Sino-Japanese war, Germany took a wait and see approach.
In the lead up to the Sino-Japanese war, Germany had become trade partners with China as well as providing military training. With war breaking out, Germany was concerned with what the war would do to German interests located in China; a prolonged war would damaged economic interests and would weaken Japan. A weak Japan would reduce the threat to Russia and the Western Europeans allowing them to concentrate more of their efforts on Europe instead of their Asian colonies. Hitler met with von Neurath and von Blomberg to restate their stance on the war and referenced the Anti-Comintern Pact. The war with China would weak both countries and allow Russians and pro-Communism to spread through the region.
Joachim von Ribbentrop, the architect of the Anti-Comintern pact, met with Oshima Hiroshi to discuss the war and possibility of extending the pact. Oshima managed to convince von Ribbentrop that the war was going well in Japan's favor. Von Ribbentrop informed Hitler and Goering this information, changing their opinion of the conflict and choosing Japan over neutrality as well as persuading and extensions of the pact to include Italy, with who Japan had been working with to remove support from the Chinese army. However, the War Ministry and Foreign Ministry were against alienating China and continued providing support.
By late 1937, Japan has defeated the Chinese in the north and had succeeded in conquering Shanghai. At this point, Japan was worried about fighting a protracted war with the Chinese. Japan had been attempting to persuade the Germans to recall their military advisers as well as halt their supply of goods to China. However, Germany was reluctant to remove advisers for fear of Soviet advisers replacing them. During this time, Germany asked for both sides to began peace talks in hopes of preserving both relationships.
At the end of 1937, Japanese managed to take Nanking and bolstered Japan to remit their demands to China. At this time, Japan was experiencing their own struggle. The Japanese military was hoping for peace while the Prime Minister did not. The Japanese Prime Minister Konoye Fumimaro managed to outmaneuver his political opponents and on in January of 1938, declared total war with China. This declaration put Germany in a difficult scenario, they would have to make a decision. Outright refusing to side with China because of his own preference, Hitler had two choices in front of him: maintaining neutrality and hoping that their interests would remain, or side with Japan. During this time in Germany, von Blomberg, the Minister of War, and Von Neurath, the Foreign minister, were both replaced in their positions and von Ribbentrop would ascend as the new Foreign Minister.
On Feburary 20th 1938, Hitler announced his official recognition of Manchukuo, the Japanese controlled portion of China, and support for Japan in the war. In May of 1938, von Ribbentrop ordered the total recall of military advisers to the Chinese forces. Germany was hoping that by appeasing the Japanese government, they could be afforded the same concessions that the Japanese offered themselves in Manchukuo. The Japanese government was however, reluctant to offer them a position better than other foreigners would expect. The Japanese Ambassador to Germany, Togo Shgenori sent von Ribbertrop text regarding the position which German interests would hold within Japanese controlled territories. Japan was unwilling to form closer ties with Germany for fear that they would anger the Soviets or the British, and Japan was not prepared to fight another war after the Sino-Japanese war.
In the summer of 1939, Germany signed the Molotov-Ribbentrop pact agreeing to non-aggression. This effectively made the Anti-Comintern pact a moot point. By the fall of 1939, Germany and the Soviet Union has invaded and divided Poland and Britain and France had declared war on Germany. Also, in the fall of 1939, an agreement for cultural cooperation between Japan and Germany was ratified by the Privy Council in Japan. Within Japan, the Cabinet was once again apprehensive of the true intentions of such a pact, they were wary of the connotations that a pact like this would have or if this was indicative of an alliance.
By December of 1939, Japan had openly declared it's intentions for a unified East Asia under Japan, effectively denouncing Britain and the Soviet Union. By this time, the gulf between Japan and Western Powers was widening. Prime Minister Mitsumasa Yonai, however, was not willing for the country to be drawn into the European War, so that they could have their full power concentrated on consolidating their powers in East Asia. In July of 1940, the US would pass the Export Control act, further increasing Japan's distance from Western Powers. On September 2nd of 1940, Japan invaded French Indochina. With the declaration of war on France and the end of the Yonai Cabinet, Japan entered talks to further their actions in East Asia. On September 27th of 1940, Japan signed the Tripartite Pact with Germany and Italy establishing the Axis forces. This was understood by each country as a means to improve their own aggressiveness as well as deter external forces from stepping into the war.
With the pact signed, Nazi Germany and Japan began to support each other economically and militarily. Germany sent technicians and engineers to Japan and helped build the IJA's air force. On November 11th of 1940, the German cruiser Atlantis boarded the SS Automedon and uncovered secret communiques from the British Far East Command. This detailed the British position in Singapore and their ineffectiveness with dealing with a war with Japan. This was shared with the Japanese, and helped greatly with their preparations to beginning war with Western Powers.
In the early months of 1941, Hitler had finished preparations for Operation Barbossa and suggested that Japan attack the Soviets as well. However, with the protracted war in China and the increasing pressure from the US, Japan was reluctant to begin fighting a 2 front war. On April 13th of 1941, Japan and the Soviet Union signed the Soviet-Japanese Neutrality Pact, which would last until 1945.
By fall of 1941, Japan had pushed to South East Asia and began acquiring more territory. With these actions, the US enacted a full oil embargo on Japan effectively cutting off it's entire oil supply. With an increasing need for natural resources and the increasing threat of the US Navy, Japan entered negotiations to recover their oil. Japan offered to remove their troops from French Indochina and the US countered, however, before the counter offer could be heard, the IJN had been dispatched towards Pearl Harbor.
On Decemeber 7th of 1941, Japan attacked Pearl Harbor and brought the US into the war. Germany was pleased with the act as this would prevent the US from providing more help to the Soviets as Operation Barbarossa was not going as intended. 3 days after the declaration of war by the US on Japan, Germany declared war on the US. This was done in hopes of reciprocity from the Japanese in declaring war on the Soviets. This was however, not realized.
As the war continued to rage, all trade paths between Germany and Japan has deteriorated. The Trans-Siberian railroad had become enemy territory and the waters around Japan had become swarmed with the British and American navies. However, with the conquest of North Africa, Germany was able to attempt to transport goods through the Suez Canal. With the defeat at the Battle of Midway, Japan's need for resources had increased.
In early 1943, the Treaty on Economic Cooperation was passed, but it was basically as good as the paper it was written on. Even though, the Suez Canal was once again usable, the British Navy was still controlling most of it. The only option to supply goods to Japan was via submarines.
By 1945, Germany and Japan had both suffered great losses and were unable to continue much further. By May, Germany falls. By September, Japan surrenders.